[英]How to run Socket.IO client-server in Java
我試圖在Java中運行一個簡單的client-server socket.io
,但是client無法建立連接。
我已經為客戶端線程https://github.com/socketio/socket.io-client-java實現了socket.io的實現
在服務器線程上,我正在使用https://github.com/mrniko/netty-socketio
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class BinaryEventLauncher {
static private Socket socket;
static final int PORT = 9292;
static SocketIOServer server;
public static void main(String[] args) throws InterruptedException {
Thread ts = new Thread(new Runnable() {
@Override
public void run() {
try {
server();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
Thread tc = new Thread(new Runnable() {
@Override
public void run() {
try {
client();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
ts.start();
tc.start();
}
public static void server() throws InterruptedException, UnsupportedEncodingException {
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(PORT);
server = new SocketIOServer(config);
server.addEventListener("toServer", String.class, new DataListener<String>() {
@Override
public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
client.sendEvent("toClient", "message from server");
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}
public static void client() throws URISyntaxException, InterruptedException {
IO.Options opt = new IO.Options();
opt.port = PORT;
socket = IO.socket("http://localhost", opt);
socket.on("toClient", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(args);
}
});
while(!socket.connected()) {
socket.connect();
Thread.sleep(100);
System.out.println(socket.connected());
}
socket.emit("toServer", "test data");//NEVER
}
}
網址中的硬編碼端口號(不作為選項)。 並使用連接事件來捕獲連接建立的時間。
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class SocketCS {
static private Socket socket;
static final int PORT = 9291;
static SocketIOServer server;
public static void main(String[] args) throws InterruptedException {
Thread ts = new Thread(new Runnable() {
@Override
public void run() {
try {
server();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
ts.start();
try {
client();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public static void server() throws InterruptedException, UnsupportedEncodingException {
Configuration config = new Configuration();
config.setHostname("127.0.0.1");
config.setPort(PORT);
server = new SocketIOServer(config);
server.addEventListener("toServer", String.class, new DataListener<String>() {
@Override
public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
client.sendEvent("toClient", "server recieved " + data);
}
});
server.addEventListener("message", String.class, new DataListener<String>() {
@Override
public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
client.sendEvent("toClient", "message from server " + data);
}
});
server.start();
Thread.sleep(10000);
server.stop();
}
public static void client() throws URISyntaxException, InterruptedException {
socket = IO.socket("http://localhost:" + PORT);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
socket.emit("toServer", "connected");
socket.send("test");
}
});
socket.on("toClient", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Client recievd : " + args[0]);
}
});
socket.connect();
while (!socket.connected())
Thread.sleep(50);
socket.send("another test");
Thread.sleep(10000);
socket.disconnect();
}
}
控制台輸出->
Client recievd : server recieved connected
Client recievd : message from server test
Client recievd : message from server another test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.