我有一个要连接的套接字...使用下面的库。
https://github.com/socketio/socket.io-client-java
我可以连接到套接字,但是当我发出自定义事件("addChannel")
时,它永远不会触发我的侦听器。但是我可以在服务器上看到它成功发出了信号。 server response
我尝试了各种方法来解决此问题,但不会触发我的“发出”监听器。它应该接收一个JSON对象。
有人知道出了什么问题吗?
注意:在服务器端使用Java socket.io 1.0和socket.io 2.0。
这是我的代码:
package com.apps.mazeyar.chatapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class Rooms extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rooms);
final User user = (User) getIntent().getSerializableExtra("USER");
try {
IO.Options opts = new IO.Options();
String[] transport = new String[1];
transport[0] = "websocket";
opts.transports = transport;
opts.port = 443;
opts.secure = true;
opts.query = "s=" + user.getSessionId();
Log.i("Socket:", "Connecting to socket: " + "on port:" + opts.port);
final Socket socket = IO.socket("**************", opts);
socket.on(Socket.EVENT_CONNECT, args -> {
if (socket.connected()) {
Log.e("Socket", "Connected to socket");
socket.emit("addChannel", "10021237");
socket.on("addChannel", addChannel);
} else {
Log.e("Socket:", "No connection");
}
});
socket.on(Socket.EVENT_DISCONNECT, args -> Log.e("Socket:", "Disconnected"));
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
private Emitter.Listener addChannel = new Emitter.Listener() {
@Override
public void call(final Object...args) {
Rooms.this.runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
Log.e("Callback", "is never called");
}
});
}
};
}