简体   繁体   English

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

[英]Send parameter to socket in swift iOS app

I am making chat application in swift by using socket.io and node.js .我正在使用socket.ionode.js快速制作聊天应用程序。

I need to send along the user name parameter with one socket method.我需要使用一种套接字方法发送用户名参数。 I tried like below without sending parameter.我在没有发送参数的情况下尝试如下。 This works good but i need to add user name parameter to get specific data instead of total data.这很好用,但我需要添加用户名参数来获取特定数据而不是总数据。 sorry for silly question am new to socket concept.抱歉,愚蠢的问题是套接字概念的新手。

socket.on("updatechat") { dataArray, ack in
   print(dataArray)
}

Take a look at SocketIOClientOption , there is a parameter called connectParams . 看一下SocketIOClientOption ,有一个名为connectParams的参数。

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

So, all you need to do is to pass the parameters into the config: when your are creating your socket. 因此,您要做的就是在创建套接字时将参数传递给config:

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

If you are looking for SocketManager, you can set it with SocketIOClientConfiguration connectParams.如果您正在寻找 SocketManager,您可以使用 SocketIOClientConfiguration connectParams 进行设置。

let manager = SocketManager(socketURL: URL(string: "socket_url")!,
                            config: [.log(true),
                                     .compress,
                                     .connectParams(["token": "- your token -"])])

You have to connect via sockets using the specific parameter. 您必须使用特定参数通过套接字进行连接。 And then you will receive events only based on restrictions. 然后,您将仅基于限制接收事件。 Like you will connect sockets with an user id, and then will receive messages only for that user. 就像您将使用用户ID连接套接字,然后仅接收该用户的消息。 The implementation is different depend on the library that you are using for sockets. 实现方式有所不同,具体取决于用于套接字的库。 For swift I would recomand to use Socket.IO-Client-Swift . 为了迅速,我建议使用Socket.IO-Client-Swift

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM