将参数发送到 swift iOS 应用程序中的套接字

Cod*_*ker 0 sockets node.js ios swift

我正在使用socket.ionode.js.

我需要使用一种套接字方法发送用户名参数。我在不发送参数的情况下尝试如下。这很好用,但我需要添加用户名参数来获取特定数据而不是总数据。对不起,这个愚蠢的问题是套接字概念的新手。

socket.on("updatechat") { dataArray, ack in
   print(dataArray)
}
Run Code Online (Sandbox Code Playgroud)

dir*_*nee 6

看一看SocketIOClientOption,有一个参数叫connectParams

case connectParams([String: AnyObject]) // Dictionary whose contents will be passed with the connection.
Run Code Online (Sandbox Code Playgroud)

因此,您需要做的就是config:在创建套接字时将参数传递给。

let socket = SocketIOClient(socketURL: URL(string: "http://localhost:8080")!, config: [.connectParams(["username": "whatever"])])
Run Code Online (Sandbox Code Playgroud)