Harry
Harry

Reputation: 4773

What to write in MyServerHandler for Netty websocket

I have wrote my websocket server class and wrote ServerPiplineFactory class but I don't know to write in MyServerHandler class. MyServerHandler class is like

  public class DiscardServerHandler extends SimpleChannelUpstreamHandler {

private static final String WEBSOCKET_PATH = "/websocket";

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    //ctx.getChannel().write(msg);
    //msg.getClass();       
    if (msg instanceof HttpRequest) {
        //ctx.getChannel().write(msg);
    } else if (msg instanceof WebSocketFrame) {
        System.out.println("I am WebSocketFrame");          
    }

}

So I don't know what should I write if I receive HttpRequest and how to send it back to the browser.

So if write something like below in my jsp file

   </script>
   var WEBSOCKET_URL = "ws://localhost:8090/websocket";
   $(document).ready(function() {
   ws = new WebSocket(WEBSOCKET_URL);


  ws.onopen = function(event) {  
 alert("test");
 $('#status').text("Waiting...."); 
  };

  ws.onmessage = function(event) {
  var message = jQuery.parseJSON(event.data);
  alert(message);
 }

  var encoded = $.toJSON("test message");
  ws.send(encoded);

  });
  </script>
  <body>
  <p id="status">&nbsp;</p>
  </body>

and debug this jsp then it goes to messageReceived then I don't understand what to do then to how websocket server comunicate with server.

So if someone can help me to find the document on this or explain a bit about this that would be great.

Upvotes: 0

Views: 871

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23557

You will need to issue the handshake. Have a look at the websocket example at [1].

[1] https://github.com/netty/netty/blob/3/src/main/java/org/jboss/netty/example/http/websocketx/server/WebSocketServerHandler.java

Upvotes: 2

Related Questions