Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dcca712
http/websocket: WIP
daurnimator Dec 23, 2015
a8388bc
http/websocket: WIP
daurnimator Dec 26, 2015
d239aca
http/websocket: WIP; client side working
daurnimator Jan 20, 2016
af6f537
doc/index: Document websocket module
daurnimator Jan 20, 2016
320c399
examples/websocket_client: Add websocket example that streams BTC inf…
daurnimator Jan 20, 2016
8174bbf
http/request: Expose new_from_uri_t
daurnimator Feb 16, 2016
6a313c9
rockspec: Add websocket module
daurnimator Feb 16, 2016
6fb88bb
http/websocket: Rename websocket:read() to websocket:receive()
daurnimator Mar 14, 2016
8f0150f
http/websocket: Add protocol validation.
daurnimator Mar 15, 2016
68b3c39
http/websocket: Move protocol validation to it's own function
daurnimator Mar 15, 2016
c299515
http/websocket: Expose protocol selected
daurnimator Mar 15, 2016
57da185
http/websocket: Add initial server support
daurnimator Mar 15, 2016
31889a2
http/websocket: Don't mask close frames from server
daurnimator Mar 24, 2016
a4cc9a9
spec/websocket_spec: Start work on tests
daurnimator Mar 24, 2016
0fa3a06
http/websocket: Expose 'new' function for testing
daurnimator Mar 24, 2016
191bd38
http/websocket: Update readyState as close happens
daurnimator Mar 24, 2016
a6c0680
spec/websocket_spec: Add test of send, receive, close
daurnimator Mar 24, 2016
aa6fd7a
http/websocket: Return code as provided by called in close_helper
daurnimator Mar 24, 2016
0e26620
http/websocket: Fix bug where reserved flags could never be set
daurnimator Mar 24, 2016
d8eec35
spec/websocket_spec: Add test of reserved flags
daurnimator Mar 24, 2016
38896c9
http/websocket: Keep databuffer in websocket object so that state is …
daurnimator Mar 24, 2016
d5f1aa6
http/websocket: Don't allow use of send or receive when not in correc…
daurnimator Mar 25, 2016
4ab41dd
spec/websocket_spec: Rename variable
daurnimator Mar 25, 2016
4f0515f
spec/websocket_spec: Add test that goes via url constructor and full …
daurnimator Mar 25, 2016
81d21c4
spec/websocket_spec: Add test that passes protocols
daurnimator Mar 25, 2016
afbc8c8
doc/index: Document websocket.new_from_stream
daurnimator Mar 25, 2016
8433ccc
doc/index: Document protocols argument to websocket.new_from_uri
daurnimator Mar 25, 2016
d8332c7
http/websocket: Add timeout argument to websocket:accept()
daurnimator Mar 25, 2016
086dad4
http/websocket: Remove headers argument to websocket:accept()
daurnimator Mar 25, 2016
f99d064
doc/index: Give 'when' example in websocket:connect
daurnimator Mar 25, 2016
f456f75
doc/index: Document websocket:accept()
daurnimator Mar 25, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,95 @@ Returns the time in HTTP preferred date format (See [RFC 7231 section 7.1.1.1](h
Current version of lua-http as a string.


## http.websocket


### `new_from_uri(uri, protocols)` <!-- --> {#http.websocket.new_from_uri}

Creates a new `http.websocket` object of type `"client"` from the given URI.

- `protocols` (optional) should be a lua table containing a sequence of protocols to send to the server


### `new_from_stream(headers, stream)` <!-- --> {#http.websocket.new_from_stream}

Attempts to create a new `http.websocket` object of type `"server"` from the given request headers and stream.

- [`headers`](#http.headers) should be headers of a suspected websocket upgrade request from a HTTP 1 client.
- [`stream`](#http.h1_stream) should be a live HTTP 1 stream of the `"server"` type.

This function does **not** have side effects, and is hence okay to use tentatively.


### `websocket.close_timeout` <!-- --> {#http.websocket.close_timeout}

Amount of time (in seconds) to wait between sending a close frame and actually closing the connection.
Defaults to `3` seconds.


### `websocket:accept(protocols, timeout)` <!-- --> {#http.websocket:accept}

Completes negotiation with a websocket client.

- `protocols` (optional) should be a lua table containing a sequence of protocols to to allow from the client

Usually called after a successful [`new_from_stream`](#http.websocket.new_from_stream)


### `websocket:connect(timeout)` <!-- --> {#http.websocket:connect}

Connect to a websocket server.

Usually called after a successful [`new_from_uri`](#http.websocket.new_from_uri)


### `websocket:receive(timeout)` <!-- --> {#http.websocket:receive}

Reads and returns the next data frame plus its opcode.
Any ping frames received while reading will be responded to.

The opcode `0x1` will be returned as `"text"` and `0x2` will be returned as `"binary"`.


### `websocket:each()` <!-- --> {#http.websocket:each}

Iterator over [`websocket:receive()`](#http.websocket:receive).


### `websocket:send_frame(frame, timeout)` <!-- --> {#http.websocket:send_frame}

Low level function to send a raw frame.


### `websocket:send(data, opcode, timeout)` <!-- --> {#http.websocket:send}

Send the given `data` as a data frame.

- `data` should be a string
- `opcode` can be a numeric opcode, `"text"` or `"binary"`. If `nil`, defaults to a text frame


### `websocket:close(code, reason, timeout)` <!-- --> {#http.websocket:close}

Closes the websocket connection.

- `code` defaults to `1000`
- `reason` is an optional string


### Example

```lua
local websocket = require "http.websocket"
local ws = websocket.new_from_uri("wss://echo.websocket.org")
assert(ws:connect())
assert(ws:send("koo-eee!"))
local data = assert(ws:receive())
assert(data == "koo-eee!")
assert(ws:close())
```


## http.zlib

An abstraction layer over the various lua zlib libraries.
Expand Down
20 changes: 20 additions & 0 deletions examples/websocket_client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--[[
Example of websocket client usage

- Connects to the coinbase feed.
- Sends a subscribe message
- Prints off 5 messages
- Close the socket and clean up.
]]

local json = require "cjson"
local websocket = require "http.websocket"

local ws = websocket.new_from_uri("ws://ws-feed.exchange.coinbase.com")
assert(ws:connect())
assert(ws:send(json.encode({type = "subscribe", product_id = "BTC-USD"})))
for _=1, 5 do
local data = assert(ws:receive())
print(data)
end
assert(ws:close())
1 change: 1 addition & 0 deletions http-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ build = {
["http.tls"] = "http/tls.lua";
["http.util"] = "http/util.lua";
["http.version"] = "http/version.lua";
["http.websocket"] = "http/websocket.lua";
["http.zlib"] = "http/zlib.lua";
["http.compat.prosody"] = "http/compat/prosody.lua";
};
Expand Down
1 change: 1 addition & 0 deletions http/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ function request_methods:go(timeout)
end

return {
new_from_uri_t = new_from_uri_t;
new_from_uri = new_from_uri;
new_connect = new_connect;
new_from_stream = new_from_stream;
Expand Down
Loading