Simple Sails.js and Android Examples

I am really struggling with opening a socket connection using sails.io and android. What I'm trying to achieve at the moment is just to print socketid in the sails.js server console.

Android side:

I am using the nkzwa library socket.io.client (compile 'com.github.nkzawa: socket.io-client: 0.4.2')

This is the code I use in android inside AsyncTask:

private Socket mSocket; { try { mSocket = IO.socket("http://192.168.0.80:3000/batches/"); } catch (URISyntaxException e) {} } @Override protected Void doInBackground(Void... params) { mSocket.connect(); mSocket.emit("getSocketID"); } 

and my batch controller looks like this:

 module.exports = { getSocketID: function(req, res) { if (!req.isSocket) return res.badRequest(); var socketId = sails.sockets.id(req.socket); // => "BetX2G-2889Bg22xi-jy" console.log(socketId) return res.ok('My socket ID is: ' + socketId); } } 

When completing the task, I thought that I would get a console log displayed in my sail instance.

Can anyone see what I'm doing wrong?

+1
source share
3 answers

I started working like this:

  private Socket mSocket; { try { mSocket = IO.socket("http://192.168.0.80:3000"); } catch (URISyntaxException e) { throw new RuntimeException(e); } } JSONObject obj1 = new JSONObject(); try { obj1.put("url","/batches/getSocketID"); } catch (JSONException e) { e.printStackTrace(); } mSocket.emit("get",obj1); mSocket.connect(); 
+4
source

Can you try creating a node client first and trying to plug in a socket? I'm not sure that you have confidence that the server side in the sails is working correctly. I'm right?

+1
source

To make a request, use url as event and to get a response, use ack in emit .

If you find a followig Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request change http://192.168.11.111:1337 to http://192.168.11.111:1337?__sails_io_sdk_version=0.13.5

 private Socket mSocket; { try { mSocket = IO.socket("http://192.168.0.80:3000"); } catch (URISyntaxException e) { throw new RuntimeException(e); } } JSONObject jsonObject = new JSONObject(); try { jsonObject.put("url", "/records"); } catch (JSONException e) { e.printStackTrace(); } mSocket.emit("get", jsonObject, new Ack() { @Override public void call(Object... args) { Log.d(TAG, "records: " + args[0].toString()); } }); 
+1
source

Source: https://habr.com/ru/post/988676/


All Articles