【问题标题】:Establishing a WebSocket client connection in Jetty?在 Jetty 中建立 WebSocket 客户端连接?
【发布时间】:2014-08-07 20:55:20
【问题描述】:

我正在按照本教程建立与服务器的 WebSocket 连接: http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html

代码(与教程相同):

import java.net.URI;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;

/**
 * Example of a simple Echo Client.
 */
public class SimpleEchoClient {

    public static void main(String[] args) {
        String destUri = "ws://echo.websocket.org";
        if (args.length > 0) {
            destUri = args[0];
        }
        WebSocketClient client = new WebSocketClient();
        SimpleEchoClient socket = new SimpleEchoClient();

        try {
            client.start();
            URI echoUri = new URI(destUri);
            ClientUpgradeRequest request = new ClientUpgradeRequest();
            client.connect(socket, echoUri, request);
            System.out.printf("Connecting to : %s%n", echoUri);
           // socket.awaitClose(5, TimeUnit.SECONDS);
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            try {
                client.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

错误:

2014-08-07 21:49:00.346:INFO::main: Logging initialized @86ms
org.eclipse.jetty.websocket.api.InvalidWebSocketException:
SimpleEchoClient is not a valid WebSocket object.  
Object must obey one of the following rules:  
(1) class implements org.eclipse.jetty.websocket.api.WebSocketListener or  
(2) class is annotated with @org.eclipse.jetty.websocket.api.annotations.WebSocket

at org.eclipse.jetty.websocket.common.events.EventDriverFactory.wrap(EventDriverFactory.java:145)
at org.eclipse.jetty.websocket.client.WebSocketClient.connect(WebSocketClient.java:200)
at org.eclipse.jetty.websocket.client.WebSocketClient.connect(WebSocketClient.java:144)
at SimpleEchoClient.main(SimpleEchoClient.java:31)

我不太确定我导入的 jar 文件有什么问题。也许是错误的?我正在使用这个:http://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-client/9.2.2.v20140723

肯定有更简单的方法可以通过 Jetty Websocket 建立连接并开始接收数据?

【问题讨论】:

  • "SimpleEchoClient 不是有效的 WebSocket 对象。对象必须遵守以下规则之一:(1) 类实现 org.eclipse.jetty.websocket.api.WebSocketListener 或 (2) 类被注释@org.eclipse.jetty.websocket.api.annotations.WebSocket"
  • 愚蠢的问题,我该如何解决?
  • 要么实现WebSocketListener,要么使用WebSocket注解。你已经跳过了基本的 Java,不是吗?
  • 他从文档中逐字剪切和粘贴。问题不在于代码,而在于 lib 的版本。

标签: java websocket jetty


【解决方案1】:

正如 Kayman 在评论中解释的那样,您的套接字处理程序实现存在问题,请使用此处解释的最新版本并附有示例(您使用的相同但正确)http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html

【讨论】:

    【解决方案2】:

    您使用的当前版本的文档似乎已过时。尝试回滚到更稳定的 9.2.x 版本,例如:

    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>websocket-client</artifactId>
        <version>9.2.0.RC0</version>
    </dependency>
    

    【讨论】:

    • 这不是 websocket-client 的稳定版本,这是一个早期的预发布版本(RC0。又名候选版本 0)。使用更新的东西。如9.2.3.v20140905
    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多