java实现websocket的两种⽅式实例详解
⽬录
⼀、介绍
1.两种⽅式,⼀种使⽤tomcat的websocket实现,⼀种使⽤spring的websocket
3.spring与websocket整合需要spring
4.x,并且使⽤了socketjs,对不⽀持websocket的浏览器可以模拟websocket使⽤⼆、⽅式⼀:tomcat
使⽤这种⽅式⽆需别的任何配置,只需服务端⼀个处理类,
服务器端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34package com.Socket;
import java.io.IOException;
import java.util.Map;
import urrent.ConcurrentHashMap;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import net.sf.json.JSONObject;
@ServerEndpoint("/websocket/{username}")
public class WebSocket {
private static int onlineCount = 0;
private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
private Session session;
private String username;
@OnOpen
public void onOpen(@PathParam("username") String username, Session session) throws IOException {  this.username = username;
this.session = session;
addOnlineCount();
clients.put(username, this);
System.out.println("已连接");
}
@OnClose
public void onClose() throws IOException {
subOnlineCount();
}
@OnMessage
public void onMessage(String message) throws IOException {
JSONObject jsonTo = JSONObject.fromObject(message);
if(!("To").equals("All")){
sendMessageTo("给⼀个⼈", ("To").toString());
}else{
sendMessageAll("给所有⼈");
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66  sendMessageAll("给所有⼈");
}
}
@OnError
public void onError(Session session, Throwable error) {
error.printStackTrace();
}
public void sendMessageTo(String message, String To) throws IOException {  // BasicRemote().sendText(message);
//AsyncRemote().sendText(message);
for(WebSocket item : clients.values()) {
if(item.username.equals(To) )
AsyncRemote().sendText(message);
}
}
public void sendMessageAll(String message) throws IOException {
for(WebSocket item : clients.values()) {
AsyncRemote().sendText(message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
}
public static synchronized void subOnlineCount() {
前端websocket怎么用}
public static synchronized Map<String, WebSocket> getClients() {
return clients;
}
}
客户端js
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19var websocket = null;
var username = Item("name");
//判断当前浏览器是否⽀持WebSocket
if('WebSocket'in window) {
websocket = new WebSocket("ws://"+ document.location.host + "/WebChat/websocket/"+ username + "/"+ _img); } else{
alert('当前浏览器 Not support websocket')
}
//连接发⽣错误的回调⽅法
setMessageInnerHTML("WebSocket连接发⽣错误");
};
//连接成功建⽴的回调⽅法
setMessageInnerHTML("WebSocket连接成功");
}
//接收到消息的回调⽅法
setMessageInnerHTML(event.data);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 setMessageInnerHTML(event.data);
}
//连接关闭的回调⽅法
setMessageInnerHTML("WebSocket连接关闭");
}
//监听窗⼝关闭事件,当窗⼝关闭时,主动去关闭websocket连接,防⽌连接还没断开就关闭窗⼝,server端会抛异常。beforeunload = function() {
closeWebSocket();
}
//关闭WebSocket连接
function closeWebSocket() {
websocket.close();
}
发送消息只需要使⽤websocket.send(“发送消息”),就可以触发服务端的onMessage()⽅法,当连接时,触发服务器端onOpen()⽅法,此时也可以调⽤发送消息的⽅法去发送消息。关闭websocket时,触发服
务器端onclose()⽅法,此时也可以发送消息,但是不能发送给⾃⼰,因为⾃⼰的已经关闭了连接,但是可以发送给其他⼈。
三、⽅法⼆:spring整合
WebSocketConfig.java
这个类是配置类,所以需要在spring mvc配置⽂件中加⼊对这个类的扫描,第⼀个addHandler是对正常连接的配置,第⼆个是如果浏览器不⽀持websocket,使⽤socketjs模拟websocket的连接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20package com.websocket;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.fig.annotation.EnableWebSocket;
import org.springframework.fig.annotation.WebSocketConfigurer;
import org.springframework.fig.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(chatMessageHandler(),"/webSocketServer").addInterceptors(new ChatHandshakeInterceptor());  registry.addHandler(chatMessageHandler(), "/sockjs/webSocketServer").addInterceptors(new ChatHandshakeInterceptor()).withSockJS();
}
@Bean
public TextWebSocketHandler chatMessageHandler(){
return new ChatMessageHandler();
}
}
ChatHandshakeInterceptor.java
这个类的作⽤就是在连接成功前和成功后增加⼀些额外的功能,Constants.java类是⼀个⼯具类,两个常量。
1 2 3package com.websocket;
import java.util.Map;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.server.ServerHttpRequest;
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,  Map<String, Object> attributes) throws Exception {
System.out.println("Before Handshake");
/*
* if (request instanceof ServletServerHttpRequest) {
* ServletServerHttpRequest servletRequest = (ServletServerHttpRequest)
* request; HttpSession session =
* ServletRequest().getSession(false); if (session !=
* null) { //使⽤userName区分WebSocketHandler,以便定向发送消息 String userName =
* (String) Attribute(Constants.SESSION_USERNAME); if
* (userName==null) { userName="default-system"; }
* attributes.put(Constants.WEBSOCKET_USERNAME,userName);
*
* } }
*/
//使⽤userName区分WebSocketHandler,以便定向发送消息(使⽤shiro获取session,或是使⽤上⾯的⽅式)
String userName = (String) Subject().getSession().getAttribute(Constants.SESSION_USERNAME);
if(userName == null) {
userName = "default-system";
}
attributes.put(Constants.WEBSOCKET_USERNAME, userName);
return super.beforeHandshake(request, response, wsHandler, attributes);
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception ex) {
System.out.println("After Handshake");
super.afterHandshake(request, response, wsHandler, ex);
}
}
ChatMessageHandler.java
这个类是对消息的⼀些处理,⽐如是发给⼀个⼈,还是发给所有⼈,并且前端连接时触发的⼀些动作
1 2 3 4 5 6 7 8 9 10 11 12 13package com.websocket;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class ChatMessageHandler extends TextWebSocketHandler {
private static final ArrayList<WebSocketSession> users;// 这个会出现性能问题,最好⽤Map来存储,key⽤userid  private static Logger logger = Logger(ChatMessageHandler.class);
static{
users = new ArrayList<WebSocketSession>();
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67  users = new ArrayList<WebSocketSession>();
}
/**
* 连接成功时候,会触发UI上onopen⽅法
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
System.out.println("connect to the ");
users.add(session);
// 这块会实现⾃⼰业务,⽐如,当⽤户登录后,会把离线消息推送给⽤户
// TextMessage returnMessage = new TextMessage("你将收到的离线");
// session.sendMessage(returnMessage);
}
/
**
* 在UI在⽤js调⽤websocket.send()时候,会调⽤该⽅法
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {  sendMessageToUsers(message);
//super.handleTextMessage(session, message);
}
/**
* 给某个⽤户发送消息
*
* @param userName
* @param message
*/
public void sendMessageToUser(String userName, TextMessage message) {
for(WebSocketSession user : users) {
Attributes().get(Constants.WEBSOCKET_USERNAME).equals(userName)) {
try{
if(user.isOpen()) {
user.sendMessage(message);
}
} catch(IOException e) {
e.printStackTrace();
}
break;
}
}
}
/**
* 给所有在线⽤户发送消息
*
* @param message
*/
public void sendMessageToUsers(TextMessage message) {
for(WebSocketSession user : users) {
try{
if(user.isOpen()) {
user.sendMessage(message);
}
} catch(IOException e) {
e.printStackTrace();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。