Websocket通信
websocket的概念
websocket是HTML5的一种新协议,允许服务器向客户端传递信息,实现浏览器和客户端双工通信。
全双工通信又称为双向同时通信,即通信的双方可以同时发送和接收信息的信息交互方式。
websocket的特点
- 性能开销小,通信高效
- 没有同源限制,可以与任意服务器通信
websocket的应用场景
社交订阅、多人聊天/游戏、协同编辑文档、收集访问点击数据、股票基金报价、体育实况更新、在线教育、论坛消息广播等。
websocket和http的区别
http协议是短链接,因为请求之后,都会关闭连接,下次请求需要重新打开链接。
websocket协议是一种长连接,只需要通过一次请求来初始化连接,然后所有请求和响应都是通过TCP链接进行通信。
websocket和socket的区别
socket是应用层与TCP/IP协议通信的中间软件抽象层,它是一组接口。
websocket协议是一个完整的应用层协议,拥有一套完整的API。
WebSocket中的常用注解
- @ServerEndpoint :指定WebSocket服务端接口地址,类似与servlet中的 RequestMapping
- @OnOpen:新客户端建立连接时调用标有此注解的方法,类似与servlet中的 init()初始化
- @OnClose:客户端关闭连接时调用标有此注解的方法类,似与servlet中的destroy() 销毁
- @OnMessage:每当收到消息时就调用标有此注解的方法,似与servlet中的service() 处理请求
- @OnError:连接发生错误时就调用标有此注解的方法
websocket引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
websocket配置
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
websocket客户端代码
if(typeof(WebSocket) == "undefined") {
console.log("您的浏览器不支持WebSocket");
}else{
//实现化WebSocket对象,指定要连接的服务器地址与端口 建立连接
var socketUrl="ws://localhost:8081/websocket/"+$("#userId").val();
var socket = new WebSocket(socketUrl);
//连接事件
socket.onopen = function() {
console.log("websocket已连接");
//向服务端发送消息
socket.send($("#contentText").val());
};
//获得服务端消息事件
socket.onmessage = function(msg) {
console.log(msg.data);
};
//关闭事件
socket.onclose = function() {
console.log("websocket已关闭");
};
//发生了错误事件
socket.onerror = function() {
console.log("websocket发生了错误");
}
}
websocket服务端代码
@ServerEndpoint("/websocket/{userId}")
@Component
public class WebSocket {
private static Log log= LogFactory.get(WebSocket.class);
/**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
private static int onlineCount = 0;
/**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
private static ConcurrentHashMap<String, WebSocket> webSocketMap = new ConcurrentHashMap<>();
/**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
private Session session;
/**接收userId*/
private String userId="";
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session,@PathParam("userId") String userId) {
this.session = session;
this.userId=userId;
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
webSocketMap.put(userId,this);
//加入set中
}else{
webSocketMap.put(userId,this);
//加入set中
addOnlineCount();
//在线数加1
}
log.info("用户【{}】连接,当前在线人数为:{}",userId, getOnlineCount());
try {
sendMessage("连接成功");
} catch (IOException e) {
log.error("用户【{}】网络异常!!!!!!",userId);
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if(webSocketMap.containsKey(userId)){
webSocketMap.remove(userId);
//从set中删除
subOnlineCount();
}
log.info("用户【{}】退出,当前在线人数为:{}",userId,getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
if(!StringUtils.isEmpty(message)){
try {
log.info("收到用户【{}】的消息: {}",this.userId,message);
session.getBasicRemote().sendText("收到 "+this.userId+" 的消息 "); //回复用户
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用户【{}】错误,错误原因{}",this.userId,error.getMessage());
error.printStackTrace();
}
/**
* 实现服务器主动推送
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 发送自定义消息
*/
public static void sendInfo(String message,@PathParam("userId") String userId) throws IOException {
log.info("发送消息给【{}】,消息内容: {}",userId,message);
if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){
webSocketMap.get(userId).sendMessage(message);
}else{
log.error("用户【{}】不在线!",userId);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocket.onlineCount--;
}
}