나는 websocket을 사용하는 방법과 안드로이드와 연결되는 것에 대한 간단한 서블릿을 만드는 방법을 배우려하고있다. 그러나 나는 그것을 얻지 못한다.서블릿 WebSocket 안드로이드에 의해 Acessed
의 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); }
방법 또는? 이제 서블릿에
는 :
@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
}
나는 채팅에 대한 웹에 대한 예제를 많이하지만 모든보고있다.
도움을 주셔서 감사합니다.
좋아,하지만 문제는 Android가 아니라고 생각합니다. 먼저 안드로이드에서 Autobahn 프레임 워크를 사용하고 있습니다. –
자,이 API를 사용할 수도 있습니다. – Master
고마워, MasterJ 안드로이드에서만 jwebsocket을 사용할 수 있습니까? 지금 내 서블릿이 잘 작동하기 때문입니다. 다른 질문이 있습니다. 예를 들어, 모든 클라이언트가 아닌 한 명의 클라이언트에게만 메시지를 보내는 방법과 같은 예가 있습니다. 그리고 다른 질문은 프레임 워크로 연결을 닫을지라도 서블릿이 onDisconnect를 활성화하지 않기 때문입니다. 즉, 함수는 연결 해제 된 메시지를 인쇄하지 않기 때문입니다. –