简体   繁体   English

在Android上发送socket.io消息

[英]Emiting socket.io messages on Android

I'm trying to develop an application with socket.io . 我正在尝试使用socket.io开发应用程序。 There are 2 devices and when someone touch to screen of device 1 , I need to see a message on device 2. 有2台设备,当有人触摸设备1的屏幕时,我需要在设备2上看到一条消息。

This is nodeJS server code (I'm using SocketIO v0.9.* because socket.io-java-client isn't supporting version > 1.0.0) 这是nodeJS服务器代码(我使用的是SocketIO v0.9。*,因为socket.io-java-client不支持版本> 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) : 和我的Java代码( 单击此处查看完整代码):

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. 所以这是我的问题:两个设备都成功连接到NodeJS服务器,当我触摸设备上的屏幕时,在服务器控制台上看到“ someoneclicked”消息。 But 2nd device isn't not receiving this message and nothing happened on LogCat. 但是第二台设备没有收到此消息,并且LogCat上没有任何反应。 How can i solve this problem and communicate these 2 devices with socket.io? 我该如何解决此问题,并与socket.io通讯这两个设备?

Since you are using 0.9.* version of socket.io , to broadcast messages you need to use io.sockets.emit . 由于您使用的是0.9.*版本的socket.io ,因此要广播消息,您需要使用io.sockets.emit The shortcut io.emit was introduced in 1.0 version. 快捷方式io.emit1.0版本中引入。

Change : 变更:

io.emit('tiklama', data);

to

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

Migration from 0.9 doc says: 从0.9 doc 迁移说:

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'); io.sockets.emit('eventname','eventdata');

Now: 现在:

io.emit('eventname', 'eventdata'); 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. 请注意,在两种情况下,这些消息都会到达连接到默认“ /”名称空间的所有客户端,但不会到达其他名称空间中的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM