https://github.com/socketio/socket.io-client-java
public
static
Socket
socket; //共享该连接
/**
* 连接游戏服务
*
<p>Description:
</p>
*
@throws
URISyntaxException
*/
public
static
void
connectService()
throws
URISyntaxException{
String
serviceAddress
= GetPath.getServiceAddress(); //读取配置中的服务器地址
socket
= IO.socket(serviceAddress);
socket.on(Socket.EVENT_DISCONNECT,
new
Emitter.Listener() {
@Override
public
void
call(Object...
args) {
System.out.println("断开监听连接");
}
});
//连接 或者 重连服务器的时候
EVENT_CONNECT 连接事件 Socket.IO -Java 内部会自动重连服务
socket.on(Socket.EVENT_CONNECT,
new
Emitter.Listener() {
@Override
public
void
call(Object...
args) {
JSONObject
events2
=
new
JSONObject();
JSONObject
acctValue
=
new
JSONObject();
acctValue.put("name",
"test");
acctValue.put("password",
"password");
events2.put("event",
"user.auth");
events2.put("args",
acctValue);
socket.emit("events",
events2);
// 发送验证用户
System.out.println("发送验证用户="+events2);
System.out.println("连接成功");
}
});
socket.connect();
}