Websocket示例不起作用

编程入门 行业动态 更新时间:2024-10-26 20:22:50
本文介绍了Websocket示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试运行这个例子:

import java.util.logging.Level; import java.util.logging.Logger; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value = "/chat") public class ChatServer { private static final Logger LOGGER = Logger.getLogger(ChatServer.class.getName()); @OnOpen public void onOpen(Session session) { LOGGER.log(Level.INFO, "New connection with client: {0}", session.getId()); } @OnMessage public String onMessage(String message, Session session) { LOGGER.log(Level.INFO, "New message from Client [{0}]: {1}", new Object[] {session.getId(), message}); return "Server received [" + message + "]"; } @OnClose public void onClose(Session session) { LOGGER.log(Level.INFO, "Close connection for client: {0}", session.getId()); } @OnError public void onError(Throwable exception, Session session) { LOGGER.log(Level.INFO, "Error for client: {0}", session.getId()); } }

我使用 Tomcat 7.0。 47 ,我检查链接: ws:// localhost / Tests / chat 。 我是否需要注册此websocket或在 web.xml 中添加一些内容? 任何想法为什么不适合我?

I use Tomcat 7.0.47, I check link: ws://localhost/Tests/chat. Do I need register this websocket or add some things in web.xml? Any idea why is not working for me?

推荐答案

我在Tomcat上尝试使用WebSocket API遇到了同样的问题7.0.47。客户端显示的错误消息根本没有任何帮助,我的服务器端端点从未被创建过。

I had the same problem trying to use WebSocket API on Tomcat 7.0.47. The error message being displayed client side wasn't any help at all and my server side endpoint was never being created.

在浪费了很多时间后,我发现这是因为我设置了 javax.websocket-api 的依赖关系。我正在使用默认范围为 compile 的Maven,这会导致问题,因为Tomcat有 websocket-api.jar 在其lib文件夹中。将依赖项设置为提供解决了它。

After much wasted time I found it was due to way I had set the dependency for javax.websocket-api. I'm using Maven which has the default scope as compile which causes problems as Tomcat has the websocket-api.jar in its lib folder. Setting the dependency to provided solved it.

<dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>

希望这有帮助

它也值得注意如果在Apache后面运行,你将需要 mod_proxy_wstunnel ,如果使用IIS,你将需要8.0版,因为之前的任何东西都不支持websockets。

It's also worth noting that if running behind Apache you will need mod_proxy_wstunnel and if using IIS you will need version 8.0 as anything prior does not support websockets.

更多推荐

Websocket示例不起作用

本文发布于:2023-11-15 07:05:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592226.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   不起作用   Websocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!