onmessage not invoked when using grizzly-websockets

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

onmessage not invoked when using grizzly-websockets

pford68
Hello, I'm seeing some strange behavior when using grizzly-websockets, both in 2.1 and in 1.9.x, in Glassfish 3.1.  (I should add that I am new to using web sockets.)  When I call send() on the client side, the onmessage() method on the server side is not invoked.  I see this same behavior in an example app for version 1.9.x at (VideoSharing at http://weblogs.java.net/blog/spericas/archive/2010/09/29/web-sockets-and-html5-glassfish).  If I put a breakpoint, for example, at the beginning of onmessage() in the WebSocketapplication subclass, the breakpoint is never hit.  Curiously at this points, I seem to be able to exchange data between browsers despite apparently not entering any code in my WebSocketapplication subclass.  

Furthermore, I frequently see that createSocket(), in the WebSocketApplication, in not invoked when a new WebSocket is created on the client side.  I did register the app within a servlet, and the client-side WebSockets point to that servlet URL, with a "ws" protocol.

Can anyone shed any light on how to invoke the server-side code?
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

Bugzilla from quintesse@palacio-cristal.com
Hi,

maybe a stupid suggestion, but did you enable websocket support in the server?

(See http://weblogs.java.net/blog/spericas/archive/2010/09/29/web-sockets-and-html5-glassfish#comment-177268)

-Tako


On Wed, May 18, 2011 at 20:32, pford68 <[hidden email]> wrote:
Hello, I'm seeing some strange behavior when using grizzly-websockets, both
in 2.1 and in 1.9.x, in Glassfish 3.1.  (I should add that I am new to using
web sockets.)  When I call send() on the client side, the onmessage() method
on the server side is not invoked.  I see this same behavior in an example
app for version 1.9.x at (VideoSharing at
http://weblogs.java.net/blog/spericas/archive/2010/09/29/web-sockets-and-html5-glassfish).
If I put a breakpoint, for example, at the beginning of onmessage() in the
WebSocketapplication subclass, the breakpoint is never hit.  Curiously at
this points, I seem to be able to exchange data between browsers despite
apparently not entering any code in my WebSocketapplication subclass.

Furthermore, I frequently see that createSocket(), in the
WebSocketApplication, in not invoked when a new WebSocket is created on the
client side.  I did register the app within a servlet, and the client-side
WebSockets point to that servlet URL, with a "ws" protocol.

Can anyone shed any light on how to invoke the server-side code?


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

Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

Sebastien Dionne
In reply to this post by pford68

I had the same problem once.   Onmessage was never called.   If the websockets weren't enabled the connect will fail.

On May 18, 2011 3:11 PM, "Tako Schotanus" <[hidden email]> wrote:
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

Justin Lee-4
Right.  Make sure you have websockets enabled.  I think, though, that
one thing I'm not doing that I probably should is to forcibly close the
connection if the upgrade is requested but there is no application
backing the request.  That at least would forcibly fail things from the
client side.  I'm in the midst of refactoring that code so I'll add that.

On 5/18/11 3:14 PM, Sebastien Dionne wrote:
> I had the same problem once.   Onmessage was never called.   If the
> websockets weren't enabled the connect will fail.
> On May 18, 2011 3:11 PM, "Tako Schotanus"<[hidden email]>
> wrote:
>
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

pford68
In reply to this post by Bugzilla from quintesse@palacio-cristal.com
Yes, I did enable WebSocket support in Firefox 4.  These same issues also
occurred in Chrome.  I should have included those points.

--
View this message in context: http://grizzly.1045725.n5.nabble.com/onmessage-not-invoked-when-using-grizzly-websockets-tp4407330p4407597.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

pford68
In reply to this post by Bugzilla from quintesse@palacio-cristal.com
Yes, I did enable the websockets on the server.
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

Justin Lee-4
can you attach your server code? and the js used to connect?

On 5/18/11 4:00 PM, pford68 wrote:
> Yes, I did enable the websockets on the server.
>
> --
> View this message in context: http://grizzly.1045725.n5.nabble.com/onmessage-not-invoked-when-using-grizzly-websockets-tp4407330p4407621.html
> Sent from the Grizzly - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

pford68
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 is invoked on the server side when the client creates a socket, and sometimes it isn't.  Sometimes onmessage is invoked; sometimes is isn'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
    {
        // Removed code for injecting the app into the EventService
        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>
 ...
         

Reply | Threaded
Open this post in threaded view
|

Re: onmessage not invoked when using grizzly-websockets

nielsf
In reply to this post by pford68
I had a simular problem trying this. Make sure your glassfish has the same grizzly-websockets version as your ide.