嗨,大家好我尝试使用sendBinary方法通过websocket实现发送文件的示例。我正在使用tyrus 1.8.3实现/提供程序在Tomcat 8.0.20和客户端代码中部署服务器代码。有我的服务器和客户端类:
服务器代码:
package socket;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.CloseReason;
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("/chat")
public class Server {
@OnOpen
public void onOpen(Session session) {
}
@OnClose
public void onClose(Session session,CloseReason closeReason) {
}
@OnError
public void onError(Throwable throwable){
throwable.printStackTrace();
}
@OnMessage
public void message(String message, Session session) throws IOException {
}
@OnMessage
public void message(Session session,ByteBuffer byteBuffer) throws IOException {
System.out.println(" Binary received ... ");
}
}
客户代码:
package br.com.jslsolucoes.socket.client;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import org.apache.commons.io.IOUtils;
@ClientEndpoint
public class Client {
private static CountDownLatch latch;
@OnOpen
public void onOpen(Session session) throws IOException {
InputStream inputStream = getClass().getResourceAsStream(
"/myvideo.wmv");
byte[] buffer = new byte[8 * 1024];
while (IOUtils.read(inputStream, buffer) != 0) {
session.getBasicRemote().sendBinary(ByteBuffer.wrap(buffer));
}
}
@OnClose
public void onClose(Session session,CloseReason closeReason) throws IOException {
latch.countDown();
}
@OnMessage
public void processMessage(String message) {
}
@OnError
public void onError(Throwable throwable){
throwable.printStackTrace();
}
public static void main(String[] args) throws DeploymentException,
IOException, InterruptedException {
latch = new CountDownLatch(1);
WebSocketContainer container = ContainerProvider
.getWebSocketContainer();
String uri = "ws://localhost:8081/socket/chat";
Client client = new Client();
container.connectToServer(client, URI.create(uri));
latch.await();
}
}
它的工作正常,但在大约30秒后上传时会启动异常:
java.io.ioexception : an existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:197)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
at org.apache.tomcat.util.net.NioChannel.read(NioChannel.java:140)
at org.apache.coyote.http11.upgrade.NioServletInputStream.fillReadBuffer(NioServletInputStream.java:136)
at org.apache.coyote.http11.upgrade.NioServletInputStream.doRead(NioServletInputStream.java:80)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.read(AbstractServletInputStream.java:124)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:51)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:203)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:198)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:96)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
有人可以帮我解决这个问题吗?