2

根据平台状态,应该。我正在尝试完成一个基本的 Hello World 概念证明,但我无法让我的 Java 客户端使用 PlayN 连接到我的服务器。只要我调用 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");};
    });
4

2 回答 2

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

    }
}
于 2012-10-28T11:21:32.923 回答
1

我终于找到了问题所在。PlayN 1.4 版默认使用 Draft10。我已经更新了 PlayN 的本地副本以使用:http : //mvnrepository.com/artifact/com.netiq/websocket 并将默认草稿更改为 Draft17,它可以连接到 Tomcat。

于 2012-10-29T14:12:40.460 回答