参考文章
先放上一个坑的解决办法吧。
我用Java开启一个WebSocket服务器,通过localhost是可以连接的,通过127也可以,但是换成局域网ip之后,就连不上了。
这个坑,大概花了我一周的时间,原因很简单。比方说我的宽带、wlan、vmware虚拟机,都有一个局域网ip,如下图。
通过http,不管走哪一个局域网ip,都能访问到服务器上的页面,但是websocket不一样,websocket服务器启动之后,只能绑定一个ip,至于绑定的哪一个,不知道。所以如果想要用指定的局域网ip进行测试时,可以把其他的网络适配器禁用。
WebSocketConfig
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 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws") .setAllowedOriginPatterns("*") .withSockJS(); }
@Override public void configureWebSocketTransport(WebSocketTransportRegistration registry) { }
@Override public void configureClientInboundChannel(ChannelRegistration registration) { }
@Override public void configureClientOutboundChannel(ChannelRegistration registration) {
}
@Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
}
@Override public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
}
@Override public boolean configureMessageConverters(List<MessageConverter> messageConverters) { return false; }
@Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/"); registry.enableSimpleBroker("/chatRoom"); } }
|
MessageController
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 34 35 36
| @Controller public class MessageController { private static final String ONLINE_USERS = "onlineUsers"; private static List<String> list = new LinkedList<>();
@MessageMapping("/sendMessage") @SendTo("/chatRoom") public Message sendMessage(@RequestBody Message message) { return message; }
@MessageMapping("/addUser") @SendTo("/chatRoom") public Message addUser(@RequestBody Message message, SimpMessageHeaderAccessor headerAccessor) { list.add(message.getUsername()); System.out.println(list.size()); return message; }
@MessageMapping("/delUser") @SendTo("/chatRoom") public Message delUser(@RequestBody Message message) { list.remove(message.getUsername()); System.out.println(list.size()); return message; }
@MessageMapping("/getUsers") @SendTo("/chatRoom") public Integer getUsers() { return list.size(); } }
|
WebSocketEventListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Component public class WebSocketEventListener { private static final Logger log = LoggerFactory.getLogger(WebSocketEventListener.class);
@Autowired private SimpMessageSendingOperations simpMessageTemplate;
@EventListener public void connect(SessionConnectEvent event) { log.info("正在连接..."); }
@EventListener public void connected(SessionConnectedEvent event) { log.info("已连接上!"); }
@EventListener public void disconnected(SessionDisconnectEvent event) { log.info("关闭连接!"); } }
|
运行结果,还存在一点问题,需要进行修改。