Sending Socket request from Client (iOS & Android)

2019-02-15 20:00发布

问题:

I'm trying to use socket.io with iOS and Android App, but there is some problem here. I'm asking if there is anyone who actually has solutions.

How can I send socket.io request from client (iOS, Android), I think there is socket.io libraries for iOS and Android and iOS lib has sendEvent/Message/JSON methods. However, I couldn't find a way to get the events at the sails. https://github.com/pkyeck/socket.IO-objc https://github.com/koush/AndroidAsync So is there any way that I can send socket.io event from client? So I can use it like

join: function (req, res) {
    sails.sockets.join(req.socket, req.param('room'));
    return res.ok();
  },

Is there any way that I can know whether current socket connection is from client (ios/android) or web front end?

onConnect: function(session, socket) {

    console.log(session);
    console.log(socket);

  },

when I connect socket with server, I need url to connect. I can add some params at the url. Is there any way that I can know which parameters I've used to connect with server?

Thanks.

回答1:

From iOS to Sails.js Server (Using Socket.io)

SocketIO *socket;
NSString *url = [NSString stringWithFormat:@"/v1/controller/action?param1=%@&param2=%@", @"data1", @"data2"];
NSDictionary *params = @{@"url" : url};
[socket sendEvent:@"get" withData:data];

From Android to Sails.js Server (Using AndroidAsync, thanks to ArtworkAD)

JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("url", String.format("/v1/controller/action?param1=%s&param2=%s", "data1", "data2"));
arr.put(obj);
socketClient.emit("get", arr);

Sails Controller

action: function(req, res) {
    var data1 = req.param('param1');
    var data2 = req.param('param2');
    sails.log.debug(req.socket);
    return res.ok();
}