玩! Chrome 17 框架 Websocket

发布于 2025-01-07 02:09:39 字数 2197 浏览 1 评论 0原文

我正在尝试使用 Play 设置一个简单的 Websockets 服务器!框架(1.2.4)。现在应该发生的就是客户端应该连接,接收“Hello User”消息,然后套接字应该关闭。我使用不同的浏览器得到不同的结果:Safari 按预期工作; Chrome 17 导致错误:

play.exceptions.JavaExecutionException: The outbound channel is closed
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
at play.mvc.WebSocketInvoker.invoke(WebSocketInvoker.java:28)
at play.server.PlayHandler$WebSocketInvocation.execute(PlayHandler.java:1332)
...

这是服务器端代码:

package controllers;

import play.*;
import play.mvc.*;
import play.mvc.Http.WebSocketClose;
import play.mvc.Http.WebSocketEvent;
import play.mvc.Http.WebSocketFrame;

import java.util.*;

import models.*;
import play.data.validation.*;


public class Application extends Controller {

    public static void index() {
        render();
    }

    public static class WebSocket extends WebSocketController {
        public static void hello(String name) {
            outbound.send("Hello %s!", name);
        }
    }
}

/ws 被路由到 Application.WebSocket.hello。 客户端 javascript:

window.onload = function() {
    document.getElementById('sendbutton')
        .addEventListener('click', sendMessage, false);
    document.getElementById('connectbutton')
        .addEventListener('click', connect, false);
    document.getElementById('disconnectbutton')
        .addEventListener('click', disconnect, false);
}

function writeStatus(message) {
    var html = document.createElement("div");
    html.setAttribute('class', 'message');
    html.innerHTML = message;
    document.getElementById("status").appendChild(html);
}

function connect() {

ws = new WebSocket("ws://localhost:9000/ws?name=User");

    ws.onopen = function(evt) { 
        writeStatus("connected");
    }

    ws.onclose = function(evt) {
        writeStatus("disconnected");
    }

    ws.onmessage = function(evt) {
        writeStatus("response: " + evt.data);
    }

    ws.onerror = function(evt) {
        writeStatus("error: " + evt.data);
    }
}

function disconnect() {
    ws.close();
}

function sendMessage() {
    ws.send(document.getElementById('messagefield').value);
}

握手响应是否错误?我该如何解决这个问题?

I'm trying to set up a simple Websockets server with the Play! framework (1.2.4). All that should happen right now is the client should connect, receive a "Hello User" message, then the socket should close. I am getting different results with different browsers: Safari works as expected; Chrome 17 causes an error:

play.exceptions.JavaExecutionException: The outbound channel is closed
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
at play.mvc.WebSocketInvoker.invoke(WebSocketInvoker.java:28)
at play.server.PlayHandler$WebSocketInvocation.execute(PlayHandler.java:1332)
...

Here is the server-side code:

package controllers;

import play.*;
import play.mvc.*;
import play.mvc.Http.WebSocketClose;
import play.mvc.Http.WebSocketEvent;
import play.mvc.Http.WebSocketFrame;

import java.util.*;

import models.*;
import play.data.validation.*;


public class Application extends Controller {

    public static void index() {
        render();
    }

    public static class WebSocket extends WebSocketController {
        public static void hello(String name) {
            outbound.send("Hello %s!", name);
        }
    }
}

/ws is routed to Application.WebSocket.hello.
The client-side javascript:

window.onload = function() {
    document.getElementById('sendbutton')
        .addEventListener('click', sendMessage, false);
    document.getElementById('connectbutton')
        .addEventListener('click', connect, false);
    document.getElementById('disconnectbutton')
        .addEventListener('click', disconnect, false);
}

function writeStatus(message) {
    var html = document.createElement("div");
    html.setAttribute('class', 'message');
    html.innerHTML = message;
    document.getElementById("status").appendChild(html);
}

function connect() {

ws = new WebSocket("ws://localhost:9000/ws?name=User");

    ws.onopen = function(evt) { 
        writeStatus("connected");
    }

    ws.onclose = function(evt) {
        writeStatus("disconnected");
    }

    ws.onmessage = function(evt) {
        writeStatus("response: " + evt.data);
    }

    ws.onerror = function(evt) {
        writeStatus("error: " + evt.data);
    }
}

function disconnect() {
    ws.close();
}

function sendMessage() {
    ws.send(document.getElementById('messagefield').value);
}

Is the handshake response wrong? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2025-01-14 02:09:39

尝试从 master 分支获取最新版本。 1.2.4是在最新版本的websockets协议发布之前发布的。因此,从那时起,随着浏览器添加新版本,并且网络服务器试图迎头赶上,这一直是一个不断变化的目标。

现在它应该是稳定的,因为它已经成为 W3C 的标准,并且 Websocket 支持直接来自 Netty,而不是来自 Play 本身。

Try getting the latest version from the master branch. 1.2.4 was released before the latest version of websockets protocol was released. As a result, this has been a moving target ever since, as browsers added the newer versions, and the web servers have tried to catch up.

This should now be stable as it has become a standard from W3C, and the Websocket support is direct from Netty, rather than from Play itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文