4 changed files with 130 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||
package app.bcms.jchat.config; |
|||
|
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.web.socket.server.standard.ServerEndpointExporter; |
|||
|
|||
//开启WebSocket的支持,并把该类注入到spring容器中
|
|||
@Configuration |
|||
public class WebSocketConfig { |
|||
@Bean |
|||
public ServerEndpointExporter serverEndpointExporter() { |
|||
return new ServerEndpointExporter(); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,7 @@ |
|||
package app.bcms.jchat.websocket; |
|||
|
|||
/** |
|||
* 聊天soket |
|||
*/ |
|||
public class ChatSocket { |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
package app.bcms.jchat.websocket; |
|||
|
|||
import org.apache.commons.logging.Log; |
|||
import org.apache.commons.logging.LogFactory; |
|||
|
|||
import javax.websocket.Session; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.CopyOnWriteArraySet; |
|||
|
|||
/** |
|||
* WebSocketServer管理类 |
|||
*/ |
|||
public class WebSocketManager { |
|||
private final static Log log = LogFactory.getLog(WebSocketManager.class); |
|||
|
|||
private final static CopyOnWriteArraySet<WebSocketServer> webSocketServerSet = new CopyOnWriteArraySet<>(); |
|||
|
|||
private final static ConcurrentHashMap<String, WebSocketServer> webSocketServerMap = new ConcurrentHashMap<>(); |
|||
|
|||
public static void addWebSocketServer(WebSocketServer webSocketServer){ |
|||
if (webSocketServer != null){ |
|||
webSocketServerSet.add(webSocketServer); |
|||
webSocketServerMap.put(webSocketServer.getSessionId(), webSocketServer); |
|||
} |
|||
} |
|||
|
|||
public static void removeWebSocketServer(WebSocketServer webSocketServer){ |
|||
webSocketServerSet.remove(webSocketServer); |
|||
webSocketServerMap.remove(webSocketServer.getSessionId()); |
|||
} |
|||
|
|||
/** |
|||
* 通过SessionId发送消息给特定用户 |
|||
* @param |
|||
* @param msg |
|||
*/ |
|||
public static void sentToUser(String sessionId, String msg){ |
|||
Session session = webSocketServerMap.get(sessionId).getSession(); |
|||
sentToUser(session, msg); |
|||
} |
|||
|
|||
/** |
|||
* 通过Session发送消息给特定用户 |
|||
* @param session |
|||
* @param msg |
|||
*/ |
|||
public static void sentToUser(Session session, String msg){ |
|||
if (session == null){ |
|||
log.error("不存在该Session,无法发送消息"); |
|||
return; |
|||
} |
|||
session.getAsyncRemote().sendText(msg); |
|||
} |
|||
|
|||
/** |
|||
* 发送消息给所有用户 |
|||
* @param msg |
|||
*/ |
|||
public static void sentToAllUser(String msg){ |
|||
for (WebSocketServer webSocketServer : webSocketServerSet) { |
|||
sentToUser(webSocketServer.getSession(), msg); |
|||
} |
|||
log.info("向所有用户发送WebSocket消息完毕,消息:"+ msg); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
package app.bcms.jchat.websocket; |
|||
|
|||
import org.apache.commons.logging.Log; |
|||
import org.apache.commons.logging.LogFactory; |
|||
|
|||
import javax.websocket.*; |
|||
|
|||
public class WebSocketServer { |
|||
private final static Log log = LogFactory.getLog(WebSocketServer.class); |
|||
private Session session; |
|||
|
|||
@OnOpen |
|||
public void onOpen(Session session) { |
|||
this.session = session; |
|||
WebSocketManager.sentToUser(session, "WebSocket is connected!"); |
|||
WebSocketManager.addWebSocketServer(this); |
|||
log.info("与SessionId:"+session.getId()+"建立连接"); |
|||
} |
|||
|
|||
@OnClose |
|||
public void onClose() { |
|||
WebSocketManager.removeWebSocketServer(this); |
|||
log.info("WebSocket连接关闭"); |
|||
} |
|||
|
|||
@OnMessage |
|||
public void onMessage(String message, Session session) { |
|||
log.info("来自SessionId:"+session.getId()+"的消息:{}"+message); |
|||
} |
|||
|
|||
@OnError |
|||
public void onError(Session session, Throwable error) { |
|||
log.error("Session:"+session.getId()+"的WebSocket发生错误"+error); |
|||
} |
|||
|
|||
public Session getSession() { |
|||
return session; |
|||
} |
|||
|
|||
public String getSessionId() { |
|||
return session.getId(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue