1

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.

The Finest Artist
  • 3,150
  • 29
  • 34

1 Answers1

6

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();
}
Community
  • 1
  • 1
The Finest Artist
  • 3,150
  • 29
  • 34
  • 1
    too bad AndroidAsync has almost no examples for Android in *socket.io*. I can't create the `socketClient`, don't know how.... – Francisco Corrales Morales Nov 07 '14 at 21:18
  • @Francisco Corrales Morales. You'd probably need to import a SocketIO library for android. [link] (https://github.com/socketio/socket.io-client-java) can be a good start. I have used this in several android application and works out of the box. – burntblark Sep 04 '17 at 12:13