Servlet WebSocket Acessed by Android



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

索引.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
}

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

非常感谢任何帮助。

我知道

,你对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;
    }
}

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

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

http://jwebsocket.org/

相关内容

  • 没有找到相关文章

最新更新