-
Notifications
You must be signed in to change notification settings - Fork 4
Creating Web Sockets
Hot allows the creation of WebSockets endpoints in shows through the show.websocket object.
websocket.addHandler([path:"/chatroom"]).connect { connection ->
// Do something with the connection
}The addHandler(optionsMap) take an options object. The only option actually supported is the URL path of the WebSocket
The connect(callback) method take a callback function as only parameter. That callback receives a connection object.
The connection object allows the binding of callbacks for various events like data reception, connection close,...
The data() method of the connection object allow to bind a callback to incoming data events.
connection.data ( function (data) {
// Do something with incoming data
})In order to write to the Web Socket, just use the connection.write(data) method.
connection.data { data ->
connection.write data
}This code implements an echo WebSocket returning the message sent by the client
The connection object allows to react to a closed socket event with the close method
def close():
# Do something when Web Socket is closed by the client
pass