1

我正在尝试学习如何使用 websocket 并制作一个与 Android 连接的简单 servlet,但我不明白。

index.jsp :

var ws = new WebSocket("ws://" + document.location.host + "/myws/ServletWS");
ws.onopen = function()  { }; 
ws.onclose = function() { }; 
ws.onerror = function() { log("ERROR"); }; 
ws.onmessage = function(data) { var message = data.data; }; 
function sendMessage(msg) { ws.send(msg); } 

我如何或在哪里接收来自客户的数据?

现在在 servlet 上:

@Override protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) {
  return new ConnectionWS();
}

class ConnectionWS extends MessageInbound {
  private WsOutbound outbound;
  @Override protected void onOpen(WsOutbound outbound) { 
    this.outbound = outbound; 
  }
  @Override protected void onTextMessage(CharBuffer msg) throws IOException {
    String message = msg.toString();
    ServletWS.processData(message);
  }
  public void sendMessage(String message) {
    CharBuffer cb = CharBuffer.wrap(message);
    try {
      outbound.writeTextMessage(cb);
    } catch (IOException e) {}
  }
}

public void processData(String message){
    here I have to call the sendMessage with the answer to the client
}

我在网上看到了很多例子,但都是关于聊天的。

非常感谢您的帮助。

4

2 回答 2

1

据我所知,您对 tomcat 配置以及 java Servlet 编程有基本的了解。由于 WekSocket 是 Tomcat 中新引入的,您可能需要使用最新的 tomcat 版本在其上实现 WebSocket。我为此使用了 Apache Tomcat 7.0.42。

所以我们开始吧。首先,创建一个 Servlet,它只会为请求创建一个新的 WebSocket。如果您想按会话而不是请求进行,您可能需要对其进行修改。这是示例代码。

import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;

public class WsChatServlet extends WebSocketServlet {

    private static final long serialVersionUID = 1456546233L;

    @Override
    protected StreamInbound createWebSocketInbound(String protocol,
            HttpServletRequest request) {
        return new IncomingMessageHandler();
    }
}

现在,创建一个消息处理程序类,它将独立处理每个 WebSocket 流。就是这样!

public class IncomingMessageHandler extends MessageInbound {

    private WsOutbound myoutbound;
    public IncomingMessageHandler() {

    }

    @Override
    public void onOpen(WsOutbound outbound) {
        logger.info("Open Client.");
        this.myoutbound = outbound;
    }

    @Override
    public void onClose(int status) {
        logger.info("Close Client.");           
    }

    /**
     * Called when received plain Text Message
     */
    @Override
    public void onTextMessage(CharBuffer cb) throws IOException {

    }

    /**
     * We can use this method to pass image binary data, eventually !
     */
    @Override
    public void onBinaryMessage(ByteBuffer bb) throws IOException {

    }

    public synchronized void sendTextMessage(String message) {
        try {
            CharBuffer buffer = CharBuffer.wrap(message);
            this.getMyoutbound().writeTextMessage(buffer);
            this.getMyoutbound().flush();

        } catch (IOException e) {   
        }
    }

    /**
     * Set websocket connection timeout in milliseconds,
     * -1 means never
     */
    @Override
    public int getReadTimeout() {
        return -1;
    }

    public WsOutbound getMyoutbound() {
        return myoutbound;
    }
    public void setMyoutbound(WsOutbound myoutbound) {
        this.myoutbound = myoutbound;
    }
}
于 2013-12-23T10:05:01.267 回答
0

如果没有误解,并且您想在 Android 上使用 Web 套接字,那么为您推荐的 API 是jWebSocket.

在这里获取它,希望它已经为您提供了许多您需要完成甚至更多工作的 API。

http://jwebsocket.org/

于 2013-09-07T04:13:28.743 回答