Skip to content

Creating Web Sockets

Solimando Damien edited this page Dec 10, 2015 · 5 revisions

Hot allows the creation of WebSockets endpoints in shows through the show.websocket object.

Groovy example

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,...

React to incoming data

The data() method of the connection object allow to bind a callback to incoming data events.

Example in Javascript

connection.data ( function (data) {
    // Do something with incoming data
})

Writing to the socket

In order to write to the Web Socket, just use the connection.write(data) method.

Example in Groovy

connection.data { data ->
    connection.write data
}

This code implements an echo WebSocket returning the message sent by the client

Closing The connection

The connection object allows to react to a closed socket event with the close method

Example in python

def close():
   # Do something when Web Socket is closed by the client
   pass

Clone this wiki locally