java jetty websocket,在Jetty中建立WebSocket客户端连接?

The code (same as the tutorial):

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();

}

}

}

}

Errors:

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)

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

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值