java – 在Android上发出socket.io消息

前端之家收集整理的这篇文章主要介绍了java – 在Android上发出socket.io消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用socket.io开发一个应用程序.有2个设备,当有人触摸到设备1的屏幕时,我需要在设备2上看到一条消息.

这是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);
      });
    });

和我的Java代码(整个代码click here):

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;
            }
        });

    }

所以这里我的问题是:两台设备都成功连接到NodeJS服务器,当我触摸设备上的屏幕时,我在服务器控制台上看到“有人点击”消息.但是第二个设备没有收到此消息,并且LogCat上没有任何反应.我怎样才能解决这个问题并用socket.io传达这两个设备?

最佳答案
由于您使用的是0.9.*版本的socket.io,要广播消息,您需要使用io.sockets.emit.快捷方式io.emit是在1.0版本中引入的.

变化:

io.emit('tiklama',data);

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

Migration from 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’);

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.

原文链接:https://www.f2er.com/android/431150.html

猜你在找的Android相关文章