I am using latest Starscream version of package in my chat application.Please take a look on my code snippet:-
import UIKit
import Starscream
import Network
class ViewController: UIViewController,WebSocketDelegate {
var socket: WebSocket!
var isConnected = false
let server = WebSocketServer()
var webSocketTask: URLSessionWebSocketTask!
var request: URLRequest = URLRequest(url: URL(string: "wss://dev.api.deepak.com/tools/cognitive/channel/web/client")!)
override func viewDidLoad() {
super.viewDidLoad()
request.setValue("Q6ZzGUo8q9Tta5QM0jKiZOJ03aQ3ZS", forHTTPHeaderField: "Sec-WebSocket-Protocol")
socket = WebSocket(request: request)
socket.delegate = self
socket.connect()
}
func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(let headers):
isConnected = true
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code):
isConnected = false
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let string):
print("Received text: \(string)")
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
break
case .pong(_):
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
// print(error)
isConnected = false
case .error(let error):
isConnected = false
handleError(error)
// print(error)
}
}
output :-
websocket is connected: ["Via": "kong/0.34-1-enterprise-edition", "Connection": "Upgrade", "Server": "nginx/1.15.6", "Content-Type": "text/plain; charset=UTF-8", "Strict-Transport-Security": "max-age=15724800; includeSubDomains", "X-RateLimit-Remaining-20": "499", "Access-Control-Expose-Headers": "X-Auth-Token", "X-Kong-Upstream-Latency": "375", "X-RateLimit-Limit-20": "500", "Upgrade": "websocket", "X-Kong-Proxy-Latency": "386", "Date": "Thu, 30 Jul 2020 14:28:18 GMT", "Sec-WebSocket-Protocol": "Q6ZzGUo8q9Tta5QM0jKiZOJ03aQ3ZS", "Sec-WebSocket-Accept": "mkPgQAVeipWtSB+6t7soZQKWXW4=", "Access-Control-Allow-Credentials": "true"]
After that I m sending message:-
func createChannel()
{
let message = ["request_type":"start","bot_id":"10"]
do {
let data = try JSONSerialization.data(withJSONObject: message)
if let dataString = String(data: data, encoding: .utf8){
self.socket?.write(string: dataString)
}
} catch {
print("JSON serialization failed: ", error)
}
}
it is not returning to didReceive event.
my api and expected response :=
URL : wss://dev.api.deepak.com/tools/cognitive/channel/web/client Header: Sec-WebSocket-Protocol:Q6ZzGUo8q9Tta5QM0jKiZOJ03aQ3ZS
Session start message:
Request: {"request_type":"start","bot_id":10}
Response: {"session_id":"1595350205841-86807685-dbc3","status":"success","request_type":"start"}