1

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

Android Side:

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

This is the code that I am using in android inside an 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 batchescontroller 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 running the task I thought that I would get the console log outputted in my sails instance.

Can anyone see what I am doing wrong?

dave
  • 1,410
  • 1
  • 16
  • 42
  • Always turn on debugging everywhere and attach the logs before asking a question here. What exactly do you get in both consoles? (The node console, and the adb logcat) – Tristan Foureur Apr 15 '15 at 13:13
  • node console is empty, android has nothing related. – dave Apr 15 '15 at 13:55

3 Answers3

6

I got it 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();
dave
  • 1,410
  • 1
  • 16
  • 42
  • How do you get the URL JSON output with this code ? – jaumard Oct 13 '15 at 18:16
  • 1
    for this instance there isn't a JSON reply from the server. To do that you follow a similar pattern but add a handler for returning data, something like mSocket.on("myurl", onDataReceived); then private Emitter.Listener onDataReceived = new Emitter.Listener() { @Override public void call(final Object... args) {} – dave Oct 14 '15 at 08:27
  • Thanks ! I have another problem/question about this code ^^ my web sockets work only if the user is connected. So how can I change headers to keep session between sockets and http web services ? – jaumard Oct 14 '15 at 12:07
  • Not sure on that one, probably better asking a new question for it. – dave Oct 14 '15 at 12:37
1

Can you try first create a node client and try to connect the socket? I'm not sure if you're have certain that server side in sails are worker properly. am i right?

mastervv
  • 382
  • 1
  • 4
  • 20
1

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

If you found the followig error 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());
    }
});