Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emiting socket.io messages on Android

I'm trying to develop an application with socket.io . There are 2 devices and when someone touch to screen of device 1 , I need to see a message on device 2.

This is nodeJS server code (I'm using SocketIO v0.9.* because socket.io-java-client isn't supporting version > 1.0.0)

var app = require('http').createServer()
    var io = require('socket.io').listen(1337);

    io.on('connection', function (socket) {
      socket.on('tiklama', function (data) {
        console.log(data);
        io.emit('tiklama', data);
      });
    });

and my Java code (click here for whole code) :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout anapanel = (RelativeLayout) findViewById(R.id.anapanel);
        final TextView tw = (TextView) findViewById(R.id.textView1);
        final TextView tw2 = (TextView) findViewById(R.id.textView2);
        final TextView tw4 = (TextView) findViewById(R.id.textView4);

        try {
            socket = new SocketIO("http://SERVERIPADDRESS:1337");
            socket.connect(new IOCallback() {
                @Override
                public void onMessage(JSONObject json, IOAcknowledge ack) {
                    try {
                        Log.d("x","Server said:" + json.toString(2));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onMessage(String data, IOAcknowledge ack) {
                    Log.d("x","Server said: " + data);
                }

                @Override
                public void onError(SocketIOException socketIOException) {
                    Log.d("x","an Error occured");
                    socketIOException.printStackTrace();
                }

                @Override
                public void onDisconnect() {
                    Log.d("x","Connection terminated.");
                }

                @Override
                public void onConnect() {
                    Log.d("x","Connection established");
                }

                @Override
                public void on(String event, IOAcknowledge ack, Object... args) {
                    Log.d("x","Server triggered event '" + event + "'");

                    if(event.contentEquals("tiklama")) {
                    tw4.setText("Someone touched to device 1");
                    }
                }
            });

            // This line is cached until the connection is establisched.

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        anapanel.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                if (event.getAction() == MotionEvent.ACTION_DOWN){


                    socket.emit("tiklama", "someoneclicked");
                }

                return false;
            }
        });

    }

So here my question : Both devices connecting to NodeJS server succesfully and when i touch to screen on device I saw "someoneclicked" message on server console. But 2nd device isn't not receiving this message and nothing happened on LogCat. How can i solve this problem and communicate these 2 devices with socket.io?

like image 251
Eray Avatar asked Sep 29 '22 17:09

Eray


1 Answers

Since you are using 0.9.* version of socket.io, to broadcast messages you need to use io.sockets.emit. The shortcut io.emit was introduced in 1.0 version.

Change :

io.emit('tiklama', data);

to

io.sockets.emit('tiklama', data);

Migration from 0.9 doc says:

Shortcuts In general there are some new shortcuts for common things. The old versions should still work, but shortcuts are nice.

Broadcasting to all clients in default namespace

Previously:

io.sockets.emit('eventname', 'eventdata');

Now:

io.emit('eventname', 'eventdata');

Neat. Note that in both cases, these messages reach all clients connected to the default ‘/’ namespace, but not clients in other namespaces.

like image 186
Manish Mulimani Avatar answered Oct 20 '22 13:10

Manish Mulimani