PageRenderTime 92ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 0ms

/SHJS/src/main/java/com/sunwave/framework/websocket/echo/DefaultEchoService.java

https://gitlab.com/dannyblue/danny-project
Java | 55 lines | 31 code | 9 blank | 15 comment | 3 complexity | 9fe01347dc7865c0115a377f8f6d119e MD5 | raw file
  1. /*
  2. * Copyright 2002-2013 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.sunwave.framework.websocket.echo;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.springframework.web.socket.WebSocketSession;
  20. import com.alibaba.fastjson.JSONObject;
  21. public class DefaultEchoService implements EchoService {
  22. private final String echoFormat;
  23. public DefaultEchoService(String echoFormat) {
  24. this.echoFormat = (echoFormat != null) ? echoFormat : "%s";
  25. }
  26. @Override
  27. public String getMessage(String message,WebSocketSession session) {
  28. String[] msgArray = message.split("\\|");
  29. if("CONN".equals(msgArray[0])){
  30. JSONObject data = (JSONObject)JSONObject.parse(msgArray[1]);
  31. String username = data.get("username").toString();
  32. if(username!=null){
  33. Map<String,Object> sessionMap = new HashMap<String,Object>();
  34. sessionMap.put("allAreaId", data.get("allAreaId").toString());
  35. sessionMap.put("session", session);
  36. dealConn(username,sessionMap);
  37. }
  38. }
  39. return "success";
  40. }
  41. public String dealConn(String username,Map<String,Object> sessionMap) {
  42. EchoWebSocketHandler.SESSION_MAP.put(username, sessionMap);
  43. System.out.println("websocket用户["+username+"]连接!,连接总数:"+EchoWebSocketHandler.SESSION_MAP.keySet().size());
  44. return "success";
  45. }
  46. }