当前位置: 首页 > 工具软件 > Twee.IO > 使用案例 >

socketioserver java_Socket IO Web实时推送

蒋默
2023-12-01

packagecom.gwzx.gwzxyw.socket.test;importorg.apache.log4j.Logger;importcom.corundumstudio.socketio.AuthorizationListener;importcom.corundumstudio.socketio.Configuration;importcom.corundumstudio.socketio.HandshakeData;importcom.corundumstudio.socketio.SocketIOClient;importcom.corundumstudio.socketio.SocketIOServer;importcom.corundumstudio.socketio.listener.ConnectListener;importcom.corundumstudio.socketio.listener.DisconnectListener;importcom.gwzx.gwzxyw.socket.SocketManager;public classServerDemo {private static Logger logger = Logger.getLogger(ServerDemo.class);public static voidmain(String[] args) {try{

Configuration config= newConfiguration();

config.setHostname("localhost");

config.setPort(9093);//此处做token认证

config.setAuthorizationListener(newAuthorizationListener() {

@Overridepublic booleanisAuthorized(HandshakeData data) {

System.out.println(data);return true;

}

});

SocketIOServer server= newSocketIOServer(config);

CharteventListener listner= newCharteventListener();

listner.setServer(server);//chatevent为事件名称

server.addEventListener("chatevent", ChatObject.class, listner);//连接监听器

server.addConnectListener(newConnectListener() {

@Overridepublic voidonConnect(SocketIOClient client) {

SocketManager.clietMap.put(client.getSessionId(), client);

logger.info("连接:SessionId=" + client.getSessionId() + ", 总数con:" +SocketManager.clietMap.size());

}

});//断开监听器

server.addDisconnectListener(newDisconnectListener() {

@Overridepublic voidonDisconnect(SocketIOClient client) {//TODO Auto-generated method stub

SocketManager.clietMap.remove(client.getSessionId());

logger.info("断开:SessionId=" + client.getSessionId() + ", 总数dis:" +SocketManager.clietMap.size());

}

});//启动服务

server.start();

Thread.sleep(Integer.MAX_VALUE);

server.stop();

}catch(Exception e) {

e.printStackTrace();

}

}

}

 类似资料: