【发布时间】:2018-09-16 15:56:50
【问题描述】:
我正在尝试保持服务器和我的 java 客户端之间的连接。这是我的代码:
import io.socket.client.*;
import io.socket.client.IO;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) throws URISyntaxException, InterruptedException {
IO.Options opts = new IO.Options();
opts.reconnection = true;
Socket socket = IO.socket("http://localhost:3000", opts);
socket.connect();
socket.on("disconnect", args123 -> System.out.println("disconnect"));
Thread t = new Thread(new test(socket));
t.start();
}
}
class test implements Runnable{
private Socket socket;
public test(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
Thread.sleep(3000);
socket.emit("test","test");
System.out.println("test");
} catch (Exception e) {
e.printStackTrace();
}
}
}
线程中的测试事件没有到达服务器,因为我的客户端自动断开连接。
我也为我的服务器尝试了这个选项: {传输:['websocket'],升级:假}
或设置ping间隔和超时间隔
【问题讨论】: