赞
踩
TODO
- @ServerEndpoint(value="/websocketServer", configurator=SpringConfigurator.class, encoders = { CommonMessageEncoder.class, SystemMessageEncoder.class, HistoryMessageEncoder.class})
- public class WebsocketServer {
- //存储每个客户端对应的websocketServer实例与登录名map
- private static CopyOnWriteMap<WebsocketServer, String> webSocketUsernameMap = new CopyOnWriteMap<WebsocketServer, String>();
-
- private MessageDao messageDao = (MessageDao)ContextLoader.getCurrentWebApplicationContext().getBean("messageDao");
-
- //在线成员
- private static ConcurrentLinkedQueue<String> members = new ConcurrentLinkedQueue<String>();
-
- //每个webscoket客户端与服务器会话
- private Session session;
-
- public WebsocketServer() {
- }
-
- @OnOpen
- public void onOpen(Session session) {
- this.session = session;
- }
-
- @OnClose
- public void onClose() {
- String username = webSocketUsernameMap.get(this);
- removeMember(username);
- webSocketUsernameMap.remove(this);
-
- for (WebsocketServer webSocket : webSocketUsernameMap.keySet()) {
- try {
- sendMsg(webSocket, new SystemMessageResponse(MessageType.SYS_MSG, username, "exit", members));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- @OnMessage
- public void onMessage(String message, Session session) {
- JSONObject messageObject = new JSONObject(message);
- String type = messageObject.getString("messageType");
- String content = messageObject.getString("message");
-
- if (type.equals(MessageType.COM_MSG)) {
- //群发消息
- Date time = new Date();
- Sim
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。