0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Norman, Thanks for your response. I looked at the link and found a class WebSocketServerHandshakerFactory do I need this? because if I use this I need to use 5-6 classes under websocketx package. Also I don't understand WebSocketServerIndexPage.getContent(getWebSocketLocation(req)); Its just getting content from java class do I need this also because I have jsp file instead of this java for getting the content. I am not able to completely understand WebSocketServerHandler.java class as am new to websockets and using it first time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.