0

我在我的聊天应用程序中使用了最新版本的 Starscream 软件包。请查看我的代码片段:-

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)
    }
}

输出 :-

websocket已连接:[“Via”:“kong/0.34-1-enterprise-edition”,“Connection”:“Upgrade”,“Server”:“nginx/1.15.6”,“Content-Type”:“text/普通;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”,“升级”:“websocket”,“X-Kong-代理延迟”:“386”,“日期”:“2020 年 7 月 30 日星期四 14:28:18 GMT”,“Sec-WebSocket-Protocol”:“Q6ZzGUo8q9Tta5QM0jKiZOJ03aQ3ZS”,“Sec-WebSocket-Accept”:“mkPgQAVeipWtSB+6t7soZQKWXW4=", "访问控制允许凭据": "true"]

之后我发送消息:-

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)
    }

}

它没有返回到 didReceive 事件。

我的 api 和预期的响应:=

网址:wss://dev.api.deepak.com/tools/cognitive/channel/web/client 标头:Sec-WebSocket-Protocol:Q6ZzGUo8q9Tta5QM0jKiZOJ03aQ3ZS

会话开始消息:

请求:{"request_type":"start","bot_id":10}

响应:{"session_id":"1595350205841-86807685-dbc3","status":"success","re​​quest_type":"start"}

你能指导我我做错了什么吗?

4

0 回答 0