users@grizzly.java.net

Re: onmessage not invoked when using grizzly-websockets

From: pford68 <pford68_at_earthlink.net>
Date: Thu, 19 May 2011 11:06:28 -0700 (PDT)

Yes, I have snippets from several files below. It is all pretty rudimentary
at this point, just trying to get it to work. I should add that I have had
the same issues with a sample app (VideoSharing) from java.net:
http://today.java.net/article/2010/04/26/html5-server-push-technologies-part-2.
That is, i have often had the same issues with that app. I deployed the
same to another computer at home this morning (also using Glassfish 3,1
witrh FF 4.0 and Chrome), and the onmessage method was invoked. So far in
my experience, sometimes createSocket and onmessage are invoked on the
server side when the client creates a socket, and sometimes they aren't.

Some names have been changed below to protect the innocent. You will see
references below to injecting the WebSocketApplication into a Spring REST
service. When I try that, the WebSocketApplication's getWebSockets() always
returns an empty set, so nothing is published. Regardless, when I do test
calling WebSocket.send() from the browsers, I generally do not enter
onMessage() on the server side, and createSocket was generally not invoked.

//================================ web.xml
<servlet>
        <servlet-name>eventServlet</servlet-name>
        <servlet-class>jccc.core.EventServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>
...
<servlet-mapping>
        <servlet-name>eventServlet</servlet-name>
        <url-pattern>/jccevent</url-pattern>
</servlet-mapping>


//=========================== EventServlet
public class EventServlet extends HttpServlet
{
    private final EventWebSocketApplication app = new
EventWebSocketApplication();

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        WebSocketEngine.getEngine().register(app);
    }
}

//============================== EventWebSocketApplication
package jccc.sockets;

import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.http.HttpRequestPacket;
import org.glassfish.grizzly.websockets.WebSocket;
import org.glassfish.grizzly.websockets.WebSocketApplication;
import org.glassfish.grizzly.websockets.WebSocketListener;
import org.springframework.stereotype.Component;
...

@Component // I am injecting this in a REST service to see if I can publish
from that service.
public class EventWebSocketApplication extends WebSocketApplication
{
        // This method is usually not called when the browser creates a web socket,
        // which causes this.getWebSockets() to return an empty set.
    @Override
    public WebSocket createSocket(Connection conn, WebSocketListener...
listeners)
    {
        return new EventWebSocket(conn, listeners);
    }

    @Override
    public boolean isApplicationRequest(HttpRequestPacket httpRequestPacket)
    {
        return true;
    }

        // This method is usually not being called when the browser calls send() on
the WebSocket.
    @Override
    public void onMessage(WebSocket webSocket, String message)
    {
        System.out.println("In onmessage");
        // Get the frame payload as text
        try {
            broadcast(message);
        } catch (IOException e){
            // TODO
            System.out.println("[EventWebApplication] Error calling
broadcast from onmessage.");
        }
    }

        // Pretty rudimentary at this point. I was hoping to call broadcast from a
REST service which does the heavy lifting,
        // but getWebSockets() always returns an empty set. No one told me that I
should be able to publish in this way, so
        // maybe it shouldn't work, but I'd like to know why not.
    public void broadcast(String message) throws IOException
    {
        for (WebSocket user : this.getWebSockets())
        {
            try {
                user.send(message);
            } catch (Exception e) {
                this.onClose(user);
            }
        }
    }

}


//============================== EventWebSocket
public class EventWebSocket extends BaseWebSocket
{
    public EventWebSocket(Connection conn, WebSocketListener... listeners)
    {
        super(conn, listeners);
    }
}



//================================= control.js
// The following is called on page load. Note that I have enabled
websockets on both http-listener-1 and http-listener-2. SSL is enabled on
http-listener-2.
// Note that despite the appearance of $ below, we are not using JQuery. We
are using Ext JS as well as our own framework.
....
var instance = this,
        gridEl = this.gridPanel.getEl(),
        getComments = $.proxy(this, "getComments"),
        getUsers = $.proxy(this, "getUsers"),
        updateUserList = $.proxy(this, "updateUserList"),
        updateCommentList = $.proxy(this, "updateCommentList");
if (this.isWebSocketsEnabled()){
        if (!this.socket || this.socket.readyState == 3) {
                this.socket = new WebSocket($services.servlet); // The value of
$services.servlet on my local server is "
wss://localhost:8443/core/jcccevent"
                $.extend(this.socket, {
                        onopen: function(){
                                $.log("eventcontrol.socket onopen: " + instance.socket); // I get this
logging statement consistently when I should.
                        },
                        onclose: function(){
                                $.log("eventcontrol.socket onclose");
                        },
                        onerror: function(){
                                $.log("eventcontrol.socket error");
                        },
                        onmessage: function(response){
                                $.log("onmessage").log(response);
                                if (response.data.userList){
                                        updateUserList(response.data.userList);
                                } else if (response.data.commentList){
                                        updateCommentList(response.data.commentList);
                                }
                        }
                })
        }
} else {
        this.timer = setInterval(function(){
                getUsers();
                getComments();
        }, 30000);
        this.service = null;
}
...

// This is just a test
this.socket.send("Testing....");
}


//===================================== domain.xml
...
<network-config>
<protocols>
  <protocol name="http-listener-1">
        <http websockets-support-enabled="true" default-virtual-server="server"
max-connections="250">
          <file-cache></file-cache>
        </http>
  </protocol>
  <protocol security-enabled="true" name="http-listener-2">
        <http websockets-support-enabled="true" default-virtual-server="server"
max-connections="250">
          <file-cache></file-cache>
        </http>
        <ssl classname="com.sun.enterprise.security.ssl.GlassfishSSLImpl"
ssl3-enabled="false" cert-nickname="s1as"></ssl>
  </protocol>
 ...
          



--
View this message in context: http://grizzly.1045725.n5.nabble.com/onmessage-not-invoked-when-using-grizzly-websockets-tp4407330p4410460.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.