本文共 3894 字,大约阅读时间需要 12 分钟。
netty-socketio是基于netty的socket.io服务实现,可以无缝对接前端使用的socketio-client.js。
相对于javaee的原生websocket支持(@serverEndpoint)和spring-boot的MessageBroker(@messageMapping),netty-socketio绝对是最好用的websocket后台实现。因为netty-socketio完整的实现了socket.io提供的监听前台事件、向指定客户端发送事件、将指定客户端加入指定房间、向指定房间广播事件、客户端从指定房间退出等操作。
msg实体
packagecom.song.netty;importjava.io.Serializable;public class Msg implementsSerializable{private static final long serialVersionUID = -6519304261259719883L;privateString userId;privateString userName;privateString receiveUserId;privateString content;publicString getUserId() {returnuserId;
}public voidsetUserId(String userId) {this.userId =userId;
}publicString getUserName() {returnuserName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getReceiveUserId() {returnreceiveUserId;
}public voidsetReceiveUserId(String receiveUserId) {this.receiveUserId =receiveUserId;
}publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}publicMsg(String userId, String userName, String receiveUserId, String content) {super();this.userId =userId;this.userName =userName;this.receiveUserId =receiveUserId;this.content =content;
}publicMsg() {super();
}
}
聊天室通讯服务端可与多个客户端通讯
测试方法,在不同浏览器各打开一个聊天室页面,发送消息,全部都会接收到
监听聊天室通讯事件
packagecom.song.netty;importcom.corundumstudio.socketio.AckRequest;importcom.corundumstudio.socketio.SocketIOClient;importcom.corundumstudio.socketio.SocketIOServer;importcom.corundumstudio.socketio.listener.DataListener;public class OneToManyChartListener implements DataListener{
SocketIOServer server;public voidsetServer(SocketIOServer server) {this.server =server;
}public void onData(SocketIOClient socketIoClient, Msg msg, AckRequest askSender) throwsException {
System.out.println("一对多"+socketIoClient.getSessionId());//chatevent为 事件的名称, data为发送的内容
this.server.getBroadcastOperations().sendEvent("chatMany", msg);
}
}
点对点通讯客户端只与服务端通讯
测试方法,在不同浏览器各打开一个聊天室页面,发送消息,只有发送消息的那个客服端才能接收到消息
监听点对点通讯事件
packagecom.song.netty;importcom.corundumstudio.socketio.AckRequest;importcom.corundumstudio.socketio.SocketIOClient;importcom.corundumstudio.socketio.SocketIOServer;importcom.corundumstudio.socketio.listener.DataListener;public class OneToOneChartListener implements DataListener{
SocketIOServer server;public voidsetServer(SocketIOServer server) {this.server =server;
}public void onData(SocketIOClient socketIoClient, Msg msg, AckRequest askSender) throwsException {
System.out.println("一对一"+socketIoClient.getSessionId());//chatevent为 事件的名称, data为发送的内容
this.server.getClient(socketIoClient.getSessionId()).sendEvent("chatOne", msg);
}
}
客户端使用socket.io,首先启动server,推送消息时服务端获取客户端,向客户端发送消息。客户端接收消息后刷新页面数据。
packagecom.song.netty;importcom.corundumstudio.socketio.Configuration;importcom.corundumstudio.socketio.SocketIOServer;public classMsgServer {public static void main(String[] args) throwsInterruptedException {
Configuration config= newConfiguration();
config.setHostname("localhost");
config.setPort(8888);
SocketIOServer server= newSocketIOServer(config);
OneToOneChartListener listner= newOneToOneChartListener();
listner.setServer(server);
OneToManyChartListener listnerMany= newOneToManyChartListener();
listnerMany.setServer(server);//chatOne,chatMany为事件名称
server.addEventListener("chatOne", Msg.class, listner);
server.addEventListener("chatMany", Msg.class, listnerMany);//启动服务
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}
}
特别注意netty的版本,版本过低会导致使用socket.io通讯时跨域
com.corundumstudio.socketio
netty-socketio
1.7.14
跨域错误如下
XMLHttpRequest cannot load http://myserver/socket.io/1/?t=1400445162388. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'myclient.com' is therefore not allowed access.
前台测试页面代码一对一
聊天室}#console{height:450px;overflow:auto;
}.username-msg{color:orange;
}.connect-msg{color:green;
}.disconnect-msg{color:red;
}.send-msg{color:#888}
转载地址:http://sxspa.baihongyu.com/