我正计划编写一个流媒体类,我想为其尝试打开websocket连接,但无法使我的websocket连接正常工作。这样做的目的是使TDAmeritrade api流数据起作用。
试图使用javax.websocket-api-1.1.jar和tyrus-standalone-client-1.9
我应该使用其他的罐子吗?
package socketProg;
import java.net.URI;
import javax.websocket.*;
@ClientEndpoint
public class WSClient {
private static Object waitLock = new Object();
@OnMessage
public void onMessage(String message) {
//the new USD rate arrives from the websocket server side.
System.out.println("Received msg: "+message);
}
private static void wait4TerminateSignal()
{
synchronized(waitLock)
{try {
waitLock.wait();
} catch (InterruptedException e) {
}}}
public static void main(String[] args) {
WebSocketContainer container=null;//
Session session=null;
try{
//Tyrus is plugged via ServiceLoader API. See notes above
container = ContainerProvider.getWebSocketContainer();
//WS1 is the context-root of my web.app
//ratesrv is the path given in the ServerEndPoint annotation on server implementation
session=container.connectToServer(WSClient.class, URI.create("ws://localhost:8080/WS1/ratesrv"));
wait4TerminateSignal();
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(session!=null){
try {
session.close();
} catch (Exception e) {
e.printStackTrace();
}
我希望建立连接,但出现以下错误
java.lang.RuntimeException: Could not find an implementation class.
at
javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java: 73)
at socketProg.WSClient.main(WSClient.java:24)
对于tyrus-standalone-client-1.9出现此错误:
javax.websocket.DeploymentException: Connection failed.
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket._connect(GrizzlyClientSocket.java:404)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket.access$000(GrizzlyClientSocket.java:80)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket$1.call(GrizzlyClientSocket.java:211)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket$1.call(GrizzlyClientSocket.java:207)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket.connect(GrizzlyClientSocket.java:225)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientContainer.openClientSocket(GrizzlyClientContainer.java:72)
at org.glassfish.tyrus.client.ClientManager$3$1.run(ClientManager.java:645)
at org.glassfish.tyrus.client.ClientManager$3.run(ClientManager.java:694)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:848)
at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:493)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:299)
at socketProg.WSClient.main(WSClient.java:27)
Caused by: java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
at org.glassfish.grizzly.nio.transport.TCPNIOConnectorHandler.onConnectedAsync(TCPNIOConnectorHandler.java:187)
at org.glassfish.grizzly.nio.transport.TCPNIOConnectorHandler$1.connected(TCPNIOConnectorHandler.java:135)
at org.glassfish.grizzly.nio.transport.TCPNIOConnection.onConnect(TCPNIOConnection.java:234)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:506)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.executeIoEvent(WorkerThreadIOStrategy.java:80)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.executeIoEvent(AbstractIOStrategy.java:66)
at org.glassfish.grizzly.nio.SelectorRunner.iterateKeyEvents(SelectorRunner.java:391)
at org.glassfish.grizzly.nio.SelectorRunner.iterateKeys(SelectorRunner.java:360)
at org.glassfish.grizzly.nio.SelectorRunner.doSelect(SelectorRunner.java:324)
at org.glassfish.grizzly.nio.SelectorRunner.run(SelectorRunner.java:255)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
at java.lang.Thread.run(Unknown Source)