I'm implementing Jetty's WebSocket.OnTextMessage. When I receive message in onMessage(String), I can reply using Connection object that I acquire from onOpen(Connection) previously and the client receives it properly.
However when I want to push message from server (i.e. no initial message from client) using connection.sendMessage(String), the client does not receive it. It seems like the message is being buffered, but I can't find any method to flush it.
Does anyone know how to solve this issue? Perhaps I'm doing is incorrectly. This is what I did
public void onOpen(final Connection connection) {
this.connection = connection;
}
Then on another event
public void onReceive(BytesXMLMessage bytesXMLMessage) {
byte[] data = ((BytesMessageImpl) bytesXMLMessage).getData();
String msgToClient = new String(data);
this.connection.sendMessage(msgToClient);
System.out.println("onReceive:" + msgToClient);
}
But the message never received by the client
Update: Found the pattern, the message will never be sent to client whenever it contains HTTP methods keyword such as GET, POST, PUT, HEAD, etc irrespective of whether it is uppercase or lowercase.
Is there anyway to mitigate this issue?
Update: This is the very simplified implementation of WebSocketServlet that can simulate the issue
package com.test.websocket;
import org.eclipse.jetty.websocket.WebSocket;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class WebSocketServlet extends org.eclipse.jetty.websocket.WebSocketServlet {
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
return new WebSocket.OnTextMessage() {
private Connection connection;
@Override
public void onMessage(String data) {
try {
connection.sendMessage("Echo: " + data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onOpen(Connection connection) {
this.connection = connection;
}
@Override
public void onClose(int closeCode, String message) {
connection.close();
}
};
}
}
And this is the web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>EchoWebSocket</servlet-name>
<servlet-class>com.test.websocket.WebSocketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EchoWebSocket</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>