Websockets with Glassfish 3.1.2 and Grizzly - Unexpected response code: 405

本文介绍如何在 GlassFish 3.1.2 服务器中使用 WebSocket 技术实现视频共享应用,包括必要的配置步骤、依赖项选择及客户端与服务器端的交互代码。

 

 

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 :

<dependency> 
  <groupId>org.glassfish.grizzly</groupId> 
  <artifactId>grizzly-websockets</artifactId> 
  <version>2.2</version> 
</dependency> 

WebSocketsServlet.java

import org.glassfish.grizzly.Grizzly; 
import java.util.logging.Logger; 
import org.glassfish.grizzly.websockets.WebSocketEngine; 
 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
 
public class WebSocketsServlet extends HttpServlet { 
 
    private static final Logger logger = Grizzly.logger(WebSocketsServlet.class); 
    private final VideoSharingApplication app = new VideoSharingApplication(); ///自定义类,继承自WebSocketApplication 
 
    @Override 
    public void init(ServletConfig config) throws ServletException { 
        logger.log(Level.SEVERE, "registering"); 
        WebSocketEngine.getEngine().register(config.getServletContext().getContextPath() + "/videosharing", app); 
    }  ////
 
 voidregister(String name, WebSocketApplication app)           Deprecated. 
 voidregister(WebSocketApplication app)            

 
    @Override 
    public void destroy() { 
        WebSocketEngine.getEngine().unregister(app); 
    } 
} 

VideoSharingWebSocket.java

import java.util.logging.Logger; 
import org.glassfish.grizzly.websockets.DefaultWebSocket; 
import org.glassfish.grizzly.websockets.ProtocolHandler; 
import org.glassfish.grizzly.websockets.WebSocketListener; 
import org.glassfish.grizzly.Grizzly; 
 
public class VideoSharingWebSocket extends DefaultWebSocket { 
 
    private static final Logger logger = Grizzly.logger(VideoSharingWebSocket.class); 
 
    public VideoSharingWebSocket(ProtocolHandler handler, WebSocketListener... listeners) { 
        super(handler, listeners);    ///

 

    }  } 

VideoSharingApplication.java

import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.glassfish.grizzly.Grizzly; 
import org.glassfish.grizzly.websockets.ProtocolHandler; 
import org.glassfish.grizzly.websockets.WebSocket; 
import org.glassfish.grizzly.websockets.WebSocketApplication; 
import org.glassfish.grizzly.websockets.WebSocketListener; 
 
import org.glassfish.grizzly.http.HttpRequestPacket; 
 
public class VideoSharingApplication extends WebSocketApplication { 
 
    private static final Logger logger = Grizzly.logger(VideoSharingApplication.class); 
 
    @Override 
    public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners) { 
        logger.log(Level.SEVERE, "createSocket"); 
        return new VideoSharingWebSocket(handler, listeners); 
    } 
 
    @Override 
    public boolean isApplicationRequest(HttpRequestPacket request) { 
        logger.log(Level.SEVERE, "isApplicationRequest"); 
        return "/videosharing".equals(request.getRequestURI()); //Checks application specific criteria to determine if this application can process the Request as a WebSocket connection.检查应用程序特定的标准来确定这个应用程序可以处理该请求作为一个WebSocket连接。

    } 
 
    @Override 
    public void onMessage(WebSocket socket, String data) { 
        logger.log(Level.SEVERE, "onMessage"); 
        for (WebSocket webSocket : getWebSockets()) { 
            if (socket != webSocket) { 
                webSocket.send(data); 
            } 
        } 
    } 
} 

I enabled websockets support in Glassfish with this command :

asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true 

Client code, app.js :

var network = function () { 
    return { 
        initialize: function() { 
            var url = 'ws://localhost:8080/monApp/videosharing'; 
            var websocket = new WebSocket(url); 
            websocket.name = APP.id; 
            websocket.onopen = function(evt) { 
                alert('onopen'); 
            }; 
            websocket.onerror = function(evt) { 
                alert('onerror'); 
            }; 
            websocket.onmessage = function (evt) { 
                alert('onmessage'); 
                var command = JSON.parse(evt.data);  ///
                if (command.type == "pause") { 
                    APP.pauseVideo(); 
                } else if (command.type == "play") { 
                    APP.playVideo(); 
                } else if (command.type == "seeked") { 
                    APP.seekVideo(command.currentTime); 
                } else { 
                    alert("Unknown command " + command); 
                } 
            }; 
            websocket.onclose = function() 
            { 
                alert('onclose'); 
            }; 
        }, 
        send: function(command) { 
            websocket.send(command); 
        } 
    } 
}; 
 
var APP = { 
    id: Math.floor(Math.random() * 10000), 
 
    network: network(), 
 
    // Cannot use 'this' here after updating window.onload (see below) 
    initialize: function () { 
        APP.network.initialize(); 
        var video = APP.getVideo(); 
        video.addEventListener('play',  
            function (event) { 
                alert('play'); 
                var command = { type: "play" }; 
                APP.network.send(JSON.stringify(command)); 
            }, 
            false); 
        video.addEventListener('pause', 
            function (event) { 
                alert('pause'); 
                var command = { type: "pause" }; 
                APP.network.send(JSON.stringify(command)); 
            }, 
            false); 
        video.addEventListener('seeked', 
            function (event) { 
                alert('seeked'); 
                var command = { type: "seeked", 
                                currentTime: APP.getVideo().currentTime }; 
                APP.network.send(JSON.stringify(command)); 
            }, 
            false); 
    }, 
 
    getVideo: function () { 
        return document.getElementsByTagName("video")[0]; 
    }, 
 
    pauseVideo: function () { 
        var video = this.getVideo(); 
        video.pause(); 
    }, 
 
    playVideo: function () { 
        var video = this.getVideo(); 
        video.play(); 
    }, 
 
    seekVideo: function (currentTime) { 
        var video = this.getVideo(); 
        video.currentTime = currentTime; 
    } 
 
}; 
 
window.onload = APP.initialize; 

I'm testing this in Chrome 18.0.1025.165 in a Mac. At page loading I get this error :

Unexpected response code: 405 

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.

 

 

1 Answer

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.

share | improve this answer
It works perfectly with grizzly-websockets 1.9.46 jar and GlassFish 3.1.2. Which version of grizzly-websockets should I use to work with GlassFish 3.1 and latest version of browsers ? –  Yiseli May 2 at 14:55
I wouldn't recommend using GlassFish 3.1. GlassFish 3.1.2 has the latest WS code and supports RFC 6455. Versions prior to 3.1.2 are dated and can't be updated easily. –  rlubke May 2 at 18:01

 

 

 

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!

 

 

TypeError: DeepSeekSecureServer.start_server.<locals>.<lambda>() missing 1 required positional argument: &#39;path&#39; 2026-01-04 22:34:49,622 - websockets.server - DEBUG - > CLOSE 1011 (internal error) [2 bytes] 2026-01-04 22:34:49,623 - websockets.server - DEBUG - = connection is CLOSING 2026-01-04 22:34:49,623 - websockets.server - DEBUG - < CLOSE 1011 (internal error) [2 bytes] 2026-01-04 22:34:49,624 - websockets.server - DEBUG - > EOF 2026-01-04 22:34:49,624 - websockets.server - DEBUG - x half-closing TCP connection 2026-01-04 22:34:49,624 - websockets.server - DEBUG - < EOF 2026-01-04 22:34:49,624 - websockets.server - DEBUG - = connection is CLOSED 2026-01-04 22:34:52,640 - websockets.server - DEBUG - = connection is CONNECTING 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < GET / HTTP/1.1 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Host: 10.22.231.129:8765 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Upgrade: websocket 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Connection: Upgrade 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Sec-WebSocket-Key: y5CtVDDKaK3thLfnq7N2qw== 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Sec-WebSocket-Version: 13 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits 2026-01-04 22:34:52,641 - websockets.server - DEBUG - < User-Agent: Python/3.13 websockets/15.0.1 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > HTTP/1.1 101 Switching Protocols 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Date: Sun, 04 Jan 2026 14:34:52 GMT 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Upgrade: websocket 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Connection: Upgrade 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Sec-WebSocket-Accept: YjUsHe02598zB4ARPeTuihIWKiY= 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Sec-WebSocket-Extensions: permessage-deflate; server_max_window_bits=12; client_max_window_bits=12 2026-01-04 22:34:52,642 - websockets.server - DEBUG - > Server: Python/3.13 websockets/15.0.1 2026-01-04 22:34:52,642 - websockets.server - DEBUG - = connection is OPEN 2026-01-04 22:34:52,642 - websockets.server - INFO - connection open 2026-01-04 22:34:52,643 - websockets.server - ERROR - connection handler failed Traceback (most recent call last): File "F:\python项目\项目3.0(websocket,AI)\client\.venv\Lib\site-packages\websockets\asyncio\server.py", line 376, in conn_handler await self.handler(connection) ~~~~~~~~~~~~^^^^^^^^^^^^ TypeError: DeepSeekSecureServer.start_server.<locals>.<lambda>() missing 1 required positional argument: &#39;path&#39; 2026-01-04 22:34:52,643 - websockets.server - DEBUG - > CLOSE 1011 (internal error) [2 bytes] 2026-01-04 22:34:52,643 - websockets.server - DEBUG - = connection is CLOSING 2026-01-04 22:34:52,644 - websockets.server - DEBUG - < CLOSE 1011 (internal error) [2 bytes] 2026-01-04 22:34:52,644 - websockets.server - DEBUG - > EOF 2026-01-04 22:34:52,645 - websockets.server - DEBUG - x half-closing TCP connection 2026-01-04 22:34:52,645 - websockets.server - DEBUG - < EOF 2026-01-04 22:34:52,645 - websockets.server - DEBUG - = connection is CLOSED
最新发布
01-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值