| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | |
| | | @ServerEndpoint(value = "/api/talk/{username}") |
| | | @Component |
| | | public class WebSocketServer { |
| | | |
| | | // @Autowired |
| | | // HomeMapper homeMapper; |
| | | |
| | | static ConfigurableApplicationContext applicationContext; |
| | | |
| | | //解决无法注入mapper问题 //使用方法 homeMapper=WebSocketServer.applicationContext.getBean(HomeMapper.class); |
| | | public static void setApplicationContext(ConfigurableApplicationContext configurableApplicationContext) { |
| | | WebSocketServer.applicationContext = configurableApplicationContext; |
| | | } |
| | | |
| | | |
| | | private static final Logger log = LoggerFactory.getLogger(WebSocketServer.class); |
| | | private List<String> messages; |
| | |
| | | jsonObject.set("message", text); |
| | | this.messages.add(text); |
| | | this.sendMessage(jsonObject.toString()); //JSONUtil.toJsonStr(jsonObject) |
| | | |
| | | } |
| | | |
| | | @OnError |
| | |
| | | |
| | | public List<String> getMessages() { |
| | | return messages; |
| | | |
| | | } |
| | | |
| | | public void clearMessages() { |