在Jetty中建立WebSocket客户端连接? [英] Establishing a WebSocket client connection in Jetty?

查看:526
本文介绍了在Jetty中建立WebSocket客户端连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm following this tutorial to establish a WebSocket connection to a server: 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

I'm not too sure what is wrong with my imported jar file. Maybe it is the wrong one? I'm using this: http://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-client/9.2.2.v20140723

肯定有一种通过Jetty Websocket建立连接并开始接收数据的简便方法吗?

Surely there must be an easier way to establish a connection via Jetty Websocket and start receiving data?

推荐答案

正如凯曼(Kayman)在评论中解释的那样,您的套接字处理程序实现问题,请使用此处介绍的最新版本以及示例进行说明(与您使用的相同,但正确) http://www.eclipse.org/jetty/documentation/current /jetty-websocket-client-api.html

As Kayman explained in the comment, your problem with the socket handler implementation, use the latest release here explained with an example(same you used but correct) http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html

这篇关于在Jetty中建立WebSocket客户端连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆