I'm trying to use websockets within my local Glassfish 3.1.2 server installation. I'm using Grizzly 2.2 in my Maven project :
<code><span class="tag"><span style="color: rgb(128, 0, 0);"><dependency></span></span><span class="pln">
</span><span class="tag"><span style="color: rgb(128, 0, 0);"><groupId></span></span><span class="pln">org.glassfish.grizzly</span><span class="tag"><span style="color: rgb(128, 0, 0);"></groupId></span></span><span class="pln">
</span><span class="tag"><span style="color: rgb(128, 0, 0);"><artifactId></span></span><span class="pln">grizzly-websockets</span><span class="tag"><span style="color: rgb(128, 0, 0);"></artifactId></span></span><span class="pln">
</span><span class="tag"><span style="color: rgb(128, 0, 0);"><version></span></span><span class="pln">2.2</span><span class="tag"><span style="color: rgb(128, 0, 0);"></version></span></span><span class="pln">
</span><span class="tag"><span style="color: rgb(128, 0, 0);"></dependency></span></span><span class="pln">
</span></code>
WebSocketsServlet.java
<code><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketEngine</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">servlet</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">ServletConfig</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">servlet</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">ServletException</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> javax</span><span class="pun">.</span><span class="pln">servlet</span><span class="pun">.</span><span class="pln">http</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">HttpServlet</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pln"> </span><span style="font-size: 18px;"><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketsServlet</span></span><span class="pln"> </span></span><span class="kwd"><span style="color: rgb(0, 0, 139);">extends</span></span><span class="pln"> </span><strong><span class="typ"><span style="color: rgb(43, 145, 175);">HttpServlet</span></span><span class="pln"> </span></strong><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">private</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">static</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">final</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">.</span><span class="pln">logger</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketsServlet</span></span><span class="pun">.</span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">private</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">final</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingApplication</span></span><span class="pln"> app </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">new</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingApplication</span></span><span class="pun">();</span><span class="pln"> ///自定义类,继承自<span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketApplication</span></span><span class="pln"> </span>
</span><span class="lit"><span style="color: rgb(128, 0, 0);">@Override</span></span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">void</span></span><span class="pln"> init</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">ServletConfig</span></span><span class="pln"> config</span><span class="pun">)</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">throws</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">ServletException</span></span><span class="pln"> </span><span class="pun">{</span><span class="pln">
logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">Level</span></span><span class="pun">.</span><span class="pln">SEVERE</span><span class="pun">,</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"registering"</span></span><span class="pun">);</span><span class="pln">
</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketEngine</span></span><span class="pun">.</span><span class="pln">getEngine</span><span class="pun">().</span><span class="kwd"><span style="color: rgb(0, 0, 139);">register</span></span><span class="pun">(</span><span class="pln">config</span><span class="pun">.</span><span class="pln">getServletContext</span><span class="pun">().</span><span class="pln">getContextPath</span><span class="pun">()</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"/videosharing"</span></span><span class="pun">,</span><span class="pln"> app</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> ////</span></code><table cellspacing="0" cellpadding="3" width="100%" summary="" border="1"><tbody><tr class="TableRowColor" bgcolor="white"><td valign="top" align="right" width="1%"><span size="-1"><code> void</code></span></td><td><code><strong><a target=_blank href="http://grizzly.java.net/nonav/docs/1.9/apidocs/com/sun/grizzly/websockets/WebSocketEngine.html#register(java.lang.String, com.sun.grizzly.websockets.WebSocketApplication)" style="color: rgb(51, 102, 153); text-decoration: none;">register</a></strong>(<a target=_blank title="class or interface in java.lang" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html?is-external=true" style="color: rgb(51, 102, 153); text-decoration: none;">String</a> name, <a target=_blank title="class in com.sun.grizzly.websockets" href="http://grizzly.java.net/nonav/docs/1.9/apidocs/com/sun/grizzly/websockets/WebSocketApplication.html" style="color: rgb(51, 102, 153); text-decoration: none;">WebSocketApplication</a> app)</code>
<strong>Deprecated.</strong> </td></tr><tr class="TableRowColor" bgcolor="white"><td valign="top" align="right" width="1%"><span size="-1"><code> void</code></span></td><td><code><strong><a target=_blank href="http://grizzly.java.net/nonav/docs/1.9/apidocs/com/sun/grizzly/websockets/WebSocketEngine.html#register(com.sun.grizzly.websockets.WebSocketApplication)" style="color: rgb(51, 102, 153); text-decoration: none;">register</a></strong>(<a target=_blank title="class in com.sun.grizzly.websockets" href="http://grizzly.java.net/nonav/docs/1.9/apidocs/com/sun/grizzly/websockets/WebSocketApplication.html" style="color: rgb(51, 102, 153); text-decoration: none;">WebSocketApplication</a> app)</code>
</td></tr></tbody></table>
<code><span class="pln">
</span><span class="lit"><span style="color: rgb(128, 0, 0);">@Override</span></span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">void</span></span><span class="pln"> destroy</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketEngine</span></span><span class="pun">.</span><span class="pln">getEngine</span><span class="pun">().</span><span class="pln">unregister</span><span class="pun">(</span><span class="pln">app</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span></code>
VideoSharingWebSocket.java
<code><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">DefaultWebSocket</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">ProtocolHandler</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketListener</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingWebSocket</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">extends</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">DefaultWebSocket</span></span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">private</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">static</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">final</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">.</span><span class="pln">logger</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingWebSocket</span></span><span class="pun">.</span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingWebSocket</span></span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">ProtocolHandler</span></span><span class="pln"> handler</span><span class="pun">,</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketListener</span></span><span class="pun">...</span><span class="pln"> listeners</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">super</span></span><span class="pun">(</span><span class="pln">handler</span><span class="pun">,</span><span class="pln"> listeners</span><span class="pun">);</span><span class="pln"> ///</span></code><h3 style="margin: 0px; padding: 0px;"><a target=_blank name="t0" style="color: rgb(51, 102, 153);"></a><pre style="white-space: pre-wrap; word-wrap: break-word;">
}
}
VideoSharingApplication.java
<code><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Level</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">ProtocolHandler</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocket</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketApplication</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketListener</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">import</span></span><span class="pln"> org</span><span class="pun">.</span><span class="pln">glassfish</span><span class="pun">.</span><span class="pln">grizzly</span><span class="pun">.</span><span class="pln">http</span><span class="pun">.</span><span class="typ"><span style="color: rgb(43, 145, 175);">HttpRequestPacket</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingApplication</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">extends</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketApplication</span></span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">private</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">static</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">final</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Logger</span></span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Grizzly</span></span><span class="pun">.</span><span class="pln">logger</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingApplication</span></span><span class="pun">.</span><span class="kwd"><span style="color: rgb(0, 0, 139);">class</span></span><span class="pun">);</span><span class="pln">
</span><span class="lit"><span style="color: rgb(128, 0, 0);">@Override</span></span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocket</span></span><span class="pln"> createSocket</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">ProtocolHandler</span></span><span class="pln"> handler</span><span class="pun">,</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocketListener</span></span><span class="pun">...</span><span class="pln"> listeners</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">Level</span></span><span class="pun">.</span><span class="pln">SEVERE</span><span class="pun">,</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"createSocket"</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">return</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">new</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">VideoSharingWebSocket</span></span><span class="pun">(</span><span class="pln">handler</span><span class="pun">,</span><span class="pln"> listeners</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="lit"><span style="color: rgb(128, 0, 0);">@Override</span></span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">boolean</span></span><span class="pln"> isApplicationRequest</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">HttpRequestPacket</span></span><span class="pln"> request</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">Level</span></span><span class="pun">.</span><span class="pln">SEVERE</span><span class="pun">,</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"isApplicationRequest"</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">return</span></span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"/videosharing"</span></span><span class="pun">.</span><span class="pln">equals</span><span class="pun">(</span><span class="pln">request</span><span class="pun">.</span><span class="pln">getRequestURI</span><span class="pun">());</span><span class="pln"> //Checks application specific criteria to determine if this application can process the Request as a WebSocket connection.检查应用程序特定的标准来确定这个应用程序可以处理该请求作为一个WebSocket连接。
</span><span class="pun">}</span><span class="pln">
</span><span class="lit"><span style="color: rgb(128, 0, 0);">@Override</span></span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">public</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">void</span></span><span class="pln"> onMessage</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocket</span></span><span class="pln"> socket</span><span class="pun">,</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">String</span></span><span class="pln"> data</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
logger</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">Level</span></span><span class="pun">.</span><span class="pln">SEVERE</span><span class="pun">,</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"onMessage"</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">for</span></span><span class="pln"> </span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocket</span></span><span class="pln"> webSocket </span><span class="pun">:</span><span class="pln"> getWebSockets</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">if</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">socket </span><span class="pun">!=</span><span class="pln"> webSocket</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
webSocket</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">data</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span></code>
I enabled websockets support in Glassfish with this command :
<code><span class="pln">asadmin set configs</span><span class="pun">.</span><span class="pln">config</span><span class="pun">.</span><span class="pln">server</span><span class="pun">-</span><span class="pln">config</span><span class="pun">.</span><span class="pln">network</span><span class="pun">-</span><span class="pln">config</span><span class="pun">.</span><span class="pln">protocols</span><span class="pun">.</span><span class="pln">protocol</span><span class="pun">.</span><span class="pln">http</span><span class="pun">-</span><span class="pln">listener</span><span class="pun">-</span><span class="lit"><span style="color: rgb(128, 0, 0);">1.http</span></span><span class="pun">.</span><span class="pln">websockets</span><span class="pun">-</span><span class="pln">support</span><span class="pun">-</span><span class="pln">enabled</span><span class="pun">=</span><span class="kwd"><span style="color: rgb(0, 0, 139);">true</span></span><span class="pln">
</span></code>
Client code, app.js :
<code><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> network </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">return</span></span><span class="pln"> </span><span class="pun">{</span><span class="pln">
initialize</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> url </span><span class="pun">=</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">'ws://localhost:8080/monApp/videosharing'</span></span><span class="pun">;</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> websocket </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">new</span></span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">WebSocket</span></span><span class="pun">(</span><span class="pln">url</span><span class="pun">);</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> APP</span><span class="pun">.</span><span class="pln">id</span><span class="pun">;</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">onopen </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pun">(</span><span class="pln">evt</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'onopen'</span></span><span class="pun">);</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">onerror </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pun">(</span><span class="pln">evt</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'onerror'</span></span><span class="pun">);</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">onmessage </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">evt</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'onmessage'</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> JSON</span><span class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span class="pln">evt</span><span class="pun">.</span><span class="pln">data</span><span class="pun">);</span><span class="pln"> ///
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">if</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">command</span><span class="pun">.</span><span class="pln">type </span><span class="pun">==</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"pause"</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">pauseVideo</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">else</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">if</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">command</span><span class="pun">.</span><span class="pln">type </span><span class="pun">==</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"play"</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">playVideo</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">else</span></span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">if</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">command</span><span class="pun">.</span><span class="pln">type </span><span class="pun">==</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"seeked"</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">seekVideo</span><span class="pun">(</span><span class="pln">command</span><span class="pun">.</span><span class="pln">currentTime</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">else</span></span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">"Unknown command "</span></span><span class="pln"> </span><span class="pun">+</span><span class="pln"> command</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">onclose </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'onclose'</span></span><span class="pun">);</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
send</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pun">(</span><span class="pln">command</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
websocket</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">command</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> APP </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
id</span><span class="pun">:</span><span class="pln"> </span><span class="typ"><span style="color: rgb(43, 145, 175);">Math</span></span><span class="pun">.</span><span class="pln">floor</span><span class="pun">(</span><span class="typ"><span style="color: rgb(43, 145, 175);">Math</span></span><span class="pun">.</span><span class="pln">random</span><span class="pun">()</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span class="lit"><span style="color: rgb(128, 0, 0);">10000</span></span><span class="pun">),</span><span class="pln">
network</span><span class="pun">:</span><span class="pln"> network</span><span class="pun">(),</span><span class="pln">
</span><span class="com"><span style="color: rgb(128, 128, 128);">// Cannot use 'this' here after updating window.onload (see below)</span></span><span class="pln">
initialize</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">network</span><span class="pun">.</span><span class="pln">initialize</span><span class="pun">();</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> video </span><span class="pun">=</span><span class="pln"> APP</span><span class="pun">.</span><span class="pln">getVideo</span><span class="pun">();</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'play'</span></span><span class="pun">,</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'play'</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"play"</span></span><span class="pln"> </span><span class="pun">};</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">network</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">command</span><span class="pun">));</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">false</span></span><span class="pun">);</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'pause'</span></span><span class="pun">,</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'pause'</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"pause"</span></span><span class="pln"> </span><span class="pun">};</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">network</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">command</span><span class="pun">));</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">false</span></span><span class="pun">);</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">addEventListener</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'seeked'</span></span><span class="pun">,</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">event</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
alert</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">'seeked'</span></span><span class="pun">);</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> command </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> </span><span class="str"><span style="color: rgb(128, 0, 0);">"seeked"</span></span><span class="pun">,</span><span class="pln">
currentTime</span><span class="pun">:</span><span class="pln"> APP</span><span class="pun">.</span><span class="pln">getVideo</span><span class="pun">().</span><span class="pln">currentTime </span><span class="pun">};</span><span class="pln">
APP</span><span class="pun">.</span><span class="pln">network</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="pln">JSON</span><span class="pun">.</span><span class="pln">stringify</span><span class="pun">(</span><span class="pln">command</span><span class="pun">));</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">false</span></span><span class="pun">);</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
getVideo</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">return</span></span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementsByTagName</span><span class="pun">(</span><span class="str"><span style="color: rgb(128, 0, 0);">"video"</span></span><span class="pun">)[</span><span class="lit"><span style="color: rgb(128, 0, 0);">0</span></span><span class="pun">];</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
pauseVideo</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> video </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">this</span></span><span class="pun">.</span><span class="pln">getVideo</span><span class="pun">();</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">pause</span><span class="pun">();</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
playVideo</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> video </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">this</span></span><span class="pun">.</span><span class="pln">getVideo</span><span class="pun">();</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">play</span><span class="pun">();</span><span class="pln">
</span><span class="pun">},</span><span class="pln">
seekVideo</span><span class="pun">:</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="pln">currentTime</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd"><span style="color: rgb(0, 0, 139);">var</span></span><span class="pln"> video </span><span class="pun">=</span><span class="pln"> </span><span class="kwd"><span style="color: rgb(0, 0, 139);">this</span></span><span class="pun">.</span><span class="pln">getVideo</span><span class="pun">();</span><span class="pln">
video</span><span class="pun">.</span><span class="pln">currentTime </span><span class="pun">=</span><span class="pln"> currentTime</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
window</span><span class="pun">.</span><span class="pln">onload </span><span class="pun">=</span><span class="pln"> APP</span><span class="pun">.</span><span class="pln">initialize</span><span class="pun">;</span><span class="pln">
</span></code>
I'm testing this in Chrome 18.0.1025.165 in a Mac. At page loading I get this error :
<code><span class="typ"><span style="color: rgb(43, 145, 175);">Unexpected</span></span><span class="pln"> response code</span><span class="pun">:</span><span class="pln"> </span><span class="lit"><span style="color: rgb(128, 0, 0);">405</span></span><span class="pln">
</span></code>
In the server logs there is no error and only my "registering" (WebSocketsServlet) log is displayed.
Do you have any suggestion ?
Thank you in advance.
Best regards.
up vote
0 down vote
accepted
|
GlassFish 3.1.2 uses Grizzly 1.9.46. Grizzly 2.x is incompatible with that version of GlassFish. You'll need to use the 1.9.46 or later versions of 1.9 with 3.1.2.
|
answered
May 1 at 17:19
rlubke
26
1
|
|
|
|
What is GlassFish Server 3.1.2?
GlassFish Server 3.1.2 is the successor of the earlier 3.0.x releases, offering a modular runtime based OSGi now with fully-featured clustering with centralized administration of multiple clusters and high availability of stateful components. Developers also benefit from the simplified programming model and productivity improvements offered by Java EE 6.
This version has hundreds of bug fixes as well as updated versions for many of its sub-components: Jersey 1.11, Grizzly 1.9.46,Weld 1.1.4.Final, EclipseLink 2.3.2, Mojarra (JSF) 2.1.6, Metro 2.2, OSGi Core 4.3.0, JavaDB 10.8.1.2, OpenMQ 4.5.2 and more. It also now offers support for JDK 7 as well as for AIX. Full documentation for this release is available from this page.
GlassFish Server 3.1.2 is the fastest open source application server offering advanced features such as application versioning, application-scoped resources, and great development tool support from NetBeans 7.1.1, Eclipse and other popular IDEs.
After installing and using GlassFish 3.1.2, let us know what you think and, of course, feel free to get involved!