iosswiftwebsocketstarscream

Websocket being disconnected immediately after the connection


I'm using Starscream pod for handling chat functionality in my app.

Once the websocket is connected, it's being disconnected immediately. I don't get any messages in the log console, but the info about disconnection can be seen on the server-side.

Here is my viewDidLoad() piece of code:

override func viewDidLoad() {
   super.viewDidLoad()
   webSocket = WebSocket(url: URL(string: "ws://178.32.48.82:8088/")!)
   webSocket.delegate = self
   webSocket.connect()
}

And here is the websocket initialization code:

func websocketDidConnect(socket: WebSocketClient) {
   webSocket.write(string: "my_user_id=62")
}

The websocketDidConnect method is being called and after that is being disconnected, none of the above delegate methods called.

func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
   print("did disconnect")
}

func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
   print("received message")
}

func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
   print("did receive data")
}

What can be the reason of such behavior?


Solution

  • The problem is at your server-side code. Due to exception at server-side WebSocket is disconnected.