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 19, 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 name: string - can be any string you wish
    data: any - is what you send from the client
*/
webSocket.on(event: "myevent") { (data) in
    // in here you can write any logic
}

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 name: 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: "myevent", data: data)

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

Clone this wiki locally