gpt4 book ai didi

java - 在 Android 上发送 socket.io 消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:44:45 26 4
gpt4 key购买 nike

我正在尝试使用 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 服务器,当我触摸设备上的屏幕时,我在服务器控制台上看到“someoneclicked”消息。但是第二台设备没有收到此消息,并且 LogCat 上没有发生任何事情。我怎样才能解决这个问题并与 socket.io 通信这两个设备?

最佳答案

由于您使用的是 0.9.* 版本的 socket.io,要广播消息,您需要使用 io.sockets.emit1.0 版本引入了快捷方式io.emit

改变:

io.emit('tiklama', data);

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

Migration from 0.9医生说:

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.

关于java - 在 Android 上发送 socket.io 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com