Skip to main content

Websockets with 4.0b81 - pending / half-open connections

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
bjoernwuest
Offline
Joined: 2003-06-12

Good evening,

I am trying html5 websocket technology with Glassfish. I use the
Glassfish 4 b81 with JSR-356 support built-in. Yet, I fail to actually
really open a websocket. In google Chrome, it says that the websocket
connection is pending. When I try to send something from the browser to
the server via the websocket, I will get invalidDOMException with code
11. Sending something from the server to the client does not result in
any exception (or even log message) but message is never received at
the browser.

Here is my server-side code:

@ServerEndpoint(value="/ws") public class CWSHandler {
@OnOpen public void open(Session S) {
System.out.println("### NEW CONNECTION OPENED ###");
}

@OnMessage public void process(ByteBuffer Data, Session S) {
System.out.println("### GOT MESSAGE ###");
}

@OnError public void error(Session S, Throwable Cause) {
System.out.println("### HAVE ERROR ###");
if (null != Cause) { Cause.printStackTrace(); }
}

@OnClose public void close(Session S, CloseReason Reason) {
System.out.println("### CONNECTION CLOSED ### " +
Reason.getReasonPhrase());
}
}

And here is the client code:

var commChannel = new WebSocket(wsURI);
commChannel.onerror = function (evt) { console.log("Error: " + evt); };
commChannel.onopen = function (evt) {
console.log("Connection opened: " + evt);
try { commChannel.send("Hello world"); }
catch (err) { console.log(err); }
};
commChannel.onclose = function (evt) { console.log("Connection closed:
" + evt); };
commChannel.onmessage = function (evt) { console.log("Received message
from websocket: " + evt); };

The output on the server is:

[2013-03-26T20:23:34.949+0100] [glassfish 4.0] [INFO] [] [] [tid:
_ThreadID=30 _ThreadName=Thread-3] [timeMillis: 1364325814949]
[levelValue: 800] [[
### NEW CONNECTION OPENED ###]]

The output on the client is:
Connection opened: [object Event] line 4
Error: InvalidStateError: DOM Exception 11 line 6

What am I missing?

Regards
Bjoern

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pavel_bucek
Offline
Joined: 2008-10-24

Hi Bjoern,

can you please try running Tyrus sample? For example this one:

http://java.net/projects/tyrus/sources/source-code-repository/show/trunk...
(includes simple js client)

should do what you want. (you might need to use newer glassfish build,
there was new release of websocket-api in the meantime).

Also, you can use users@tyrus.java.net mailing list.

Regards,
Pavel

On 3/26/13 9:22 PM, bjoern.wuest@gmx.net wrote:
> Good evening,
>
>
> I am trying html5 websocket technology with Glassfish. I use the
> Glassfish 4 b81 with JSR-356 support built-in. Yet, I fail to actually
> really open a websocket. In google Chrome, it says that the websocket
> connection is pending. When I try to send something from the browser to
> the server via the websocket, I will get invalidDOMException with code
> 11. Sending something from the server to the client does not result in
> any exception (or even log message) but message is never received at
> the browser.
>
>
> Here is my server-side code:
>
> @ServerEndpoint(value="/ws") public class CWSHandler {
> @OnOpen public void open(Session S) {
> System.out.println("### NEW CONNECTION OPENED ###");
> }
>
> @OnMessage public void process(ByteBuffer Data, Session S) {
> System.out.println("### GOT MESSAGE ###");
> }
>
> @OnError public void error(Session S, Throwable Cause) {
> System.out.println("### HAVE ERROR ###");
> if (null != Cause) { Cause.printStackTrace(); }
> }
>
> @OnClose public void close(Session S, CloseReason Reason) {
> System.out.println("### CONNECTION CLOSED ### " +
> Reason.getReasonPhrase());
> }
> }
>
>
> And here is the client code:
>
> var commChannel = new WebSocket(wsURI);
> commChannel.onerror = function (evt) { console.log("Error: " + evt); };
> commChannel.onopen = function (evt) {
> console.log("Connection opened: " + evt);
> try { commChannel.send("Hello world"); }
> catch (err) { console.log(err); }
> };
> commChannel.onclose = function (evt) { console.log("Connection closed:
> " + evt); };
> commChannel.onmessage = function (evt) { console.log("Received message
> from websocket: " + evt); };
>
>
>
> The output on the server is:
>
> [2013-03-26T20:23:34.949+0100] [glassfish 4.0] [INFO] [] [] [tid:
> _ThreadID=30 _ThreadName=Thread-3] [timeMillis: 1364325814949]
> [levelValue: 800] [[
> ### NEW CONNECTION OPENED ###]]
>
>
> The output on the client is:
> Connection opened: [object Event] line 4
> Error: InvalidStateError: DOM Exception 11 line 6
>
>
>
> What am I missing?
>
> Regards
> Bjoern
>