Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Handle Socket

Roman Baitaliuk edited this page Jan 20, 2018 · 4 revisions

ClusterWS Swift Client provides an easy interface to communicate with ClusterWS server.

Please note that send and on events are used only for server <-> client communication, you won't be able to communicate with another user across different CPU and Machines. To communicate with another user across CPU and Machines use Pub/Sub system. (check client <-> client communication guide for more information)

Listen on events from server

To listen on events which are send from server you should use on method:

/**
    event: String - can be any string you wish
    data: Any - is what you send from the client
*/
webSocket.on(event: "my-event-name") { (data) in
    // your code to execute on event   
}

ClusterWS delegate methods

// called when WebSocket become open / connected
func onConnect() {}

// called on WebSocket disconnect / close event
func onDisconnect(code: Int?, reason: String?) {}

// called when WebSocket is error out
func onError(error: Error) {}

Don't forget to assign class delegate to listen for those events

webSocket.delegate = self

Send events to the server

To send message to the server you should use send method:

/**
    event: String - can be any string you wish (client must listen on this event name)
    data: Any - is what you want to send to the client
*/
webSocket.send(event: "my-event-name", data: "any-data-you-want")

Try to do not send events which starts with # or reserved disconnect, connection, error events.

Clone this wiki locally