根据平台状态,它应该。我正在尝试完成基本的Hello World概念验证,但我无法通过PlayN将我的Java客户端连接到我的服务器。一旦我调用createWebSocket(),就会调用onClose()。但是,我能够使用标准的html页面连接到我的服务器。
客户代码:
WebSocket s = PlayN.net().createWebSocket("ws://localhost:8080/test", new Net.WebSocket.Listener(){
public void onClose() {System.out.println("close");};
public void onDataMessage(ByteBuffer msg) {System.out.println("data");};
public void onError(String reason) {System.out.println("error");};
public void onOpen() {System.out.println("open");};
public void onTextMessage(String msg) { System.out.println("text");};
});
答案 0 :(得分:1)
以下是Jetty上的WebSocket Servlet实现示例:
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketFactory;
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WebSocketFactory wsFactory;
private final Set<SampleWebSocket> members = new CopyOnWriteArraySet<SampleWebSocket>();
@Override
public void init() throws ServletException {
wsFactory = new WebSocketFactory( new WebSocketFactory.Acceptor() {
@Override
public boolean checkOrigin( final HttpServletRequest request, final String origin ) {
// Allow all origins
return true;
}
@Override
public WebSocket doWebSocketConnect( final HttpServletRequest request, final String protocol ) {
return new SampleWebSocket();
}
} );
wsFactory.setBufferSize( 4096 );
wsFactory.setMaxIdleTime( 60000 );
}
@Override
protected void doGet( final HttpServletRequest request, final HttpServletResponse response ) throws IOException {
if ( wsFactory.acceptWebSocket( request, response ) ) {
return;
}
response.sendError( HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Websocket only" );
}
public class SampleWebSocket implements WebSocket.OnTextMessage {
private volatile Connection connection;
public SampleWebSocket( ) {
}
@Override
public void onClose( final int closeCode, final String message ) {
members.remove( this );
System.out.println( "onClose: closeCode=" +closeCode+ ", message: '" +message+ "'" );
}
@Override
public void onOpen( final Connection connection ) {
this.connection = connection;
System.out.println( "onOpen: connection=" +connection );
members.add( this );
// Send sample binary message back
try {
connection.sendMessage( "Sample message" );
} catch ( final IOException e ) {
e.printStackTrace();
}
}
@Override
public void onMessage( final String data ) {
System.out.println( "onMessage: data=" +data );
// Relay message to other connected clients
for ( final SampleWebSocket member: members ) {
try {
member.connection.sendMessage( data );
} catch ( final IOException e ) {
e.printStackTrace();
}
}
}
}
}
答案 1 :(得分:1)
我终于找到了问题。 PlayN 1.4版使用Draft10 dy默认值。我更新了PlayN的本地副本以使用:http://mvnrepository.com/artifact/com.netiq/websocket我将默认草稿更改为Draft17,它可以连接到Tomcat。