Module jls.net.http.WebSocket
Provides WebSocket client and upgrade handler.
Class WebSocket
WebSocket:new (url) | Creates a new WebSocket. |
webSocket:open () | Connects this WebSocket to a server. |
webSocket:sendTextMessage (message[, callback]) | Sends a message on this WebSocket. |
webSocket:readStart () | Starts receiving messages on this WebSocket. |
webSocket:readStop () | Stops receiving messages on this WebSocket. |
webSocket:onError (reason) | Called when this WebSocket has been closed due to an error, such as when some data couldn't be received or sent. |
webSocket:onTextMessage (message) | Called when a text message is received on this WebSocket. |
webSocket:onBinaryMessage (message) | Called when a binary message is received on this WebSocket. |
webSocket:onClose () | Called when this WebSocket has been closed. |
webSocket:close ([callback]) | Closes this WebSocket. |
WebSocket.UpgradeHandler:new ([protocol]) | Creates a new UpgradeHandler. |
upgradeHandler:onOpen (webSocket, exchange) | Called when a WebSocket is opened. |
WebSocket.UpgradeHandler:new (protocol, request) | Returns true if the specified request can be upgraded. |
Class WebSocket
The WebSocket class enables to connect to a server then send and receive messages.
see https://tools.ietf.org/html/rfc6455
Usage:
local event = require('jls.lang.event') local WebSocket = require('jls.net.http.WebSocket') local webSocket = WebSocket:new('ws://localhost/ws/') webSocket:open():next(function() function webSocket:onTextMessage(payload) webSocket:close() end webSocket:readStart() webSocket:sendTextMessage('Hello') end) event:loop()
- WebSocket:new (url)
-
Creates a new WebSocket.
Parameters:
- url string the URL to connect to.
Returns:
-
a new WebSocket
- webSocket:open ()
-
Connects this WebSocket to a server.
Returns:
-
jls.lang.Promise
a promise that resolves once the WebSocket is opened.
- webSocket:sendTextMessage (message[, callback])
-
Sends a message on this WebSocket.
Parameters:
- message string the message to send.
- callback function an optional callback function to use in place of promise. (optional)
Returns:
-
jls.lang.Promise
a promise that resolves once the data has been written.
- webSocket:readStart ()
- Starts receiving messages on this WebSocket.
- webSocket:readStop ()
- Stops receiving messages on this WebSocket.
- webSocket:onError (reason)
-
Called when this WebSocket has been closed due to an error,
such as when some data couldn't be received or sent.
Parameters:
- reason the error reason.
- webSocket:onTextMessage (message)
-
Called when a text message is received on this WebSocket.
Parameters:
- message string the text message.
- webSocket:onBinaryMessage (message)
-
Called when a binary message is received on this WebSocket.
Parameters:
- message string the binary message.
- webSocket:onClose ()
- Called when this WebSocket has been closed.
- webSocket:close ([callback])
-
Closes this WebSocket.
Parameters:
- callback function an optional callback function to use in place of promise. (optional)
Returns:
-
jls.lang.Promise
a promise that resolves once the WebSocket is closed.
- WebSocket.UpgradeHandler:new ([protocol])
-
Creates a new UpgradeHandler.
An HTTP handler to upgrade the connection and accept web sockets.
Parameters:
- protocol string The protocol. (optional)
Returns:
-
a new UpgradeHandler
- upgradeHandler:onOpen (webSocket, exchange)
-
Called when a WebSocket is opened.
The default implentation closes the WebSocket.
Parameters:
- webSocket WebSocket The new WebSocket.
- exchange The HTTP exchange used for the upgrade.
- WebSocket.UpgradeHandler:new (protocol, request)
-
Returns true if the specified request can be upgraded.
An HTTP handler to upgrade the connection and accept web sockets.
Parameters:
- protocol string The protocol.
- request The HTTP request.
Returns:
-
boolean
true if the specified request can be upgraded.