官方的websocket示例没有跑通,求指点,万分感激!

mac系统,websocket服务端用的go,网页示例和调试工具可以连接跑通,用咱们引擎连接失败,用的官方示例。引擎版本2.4.6和3.3.2都是一样的情况,求指点。


WebSocket instance wasn’t ready…
HelloWorld.js:20 WebSocket connection to ‘ws://localhost:8080/msg’ failed:
start @ HelloWorld.js:20
HelloWorld.js:29 Send Text fired an error
HelloWorld.js:32 WebSocket instance closed.

服务端访问地址是ws://localhost:8080/msg, cocos creator启动地址http://localhost:7456/ websocket应该没有跨域问题吧?
这个问题折腾很久,求高人指点,万分感激

代码都不给出来怎么看

let ws = new WebSocket(“ws://localhost:8080/msg”);
//用于指定连接成功后的回调函数。
ws.onopen = function () {
console.log(“Send Text WS was opened.”);
};
//用于指定当从服务器接受到信息时的回调函数。
ws.onmessage = function (event) {
console.log("response text msg: " + event.data);
};
//用于指定连接失败后的回调函数。
ws.onerror = function (event) {
console.log(“Send Text fired an error”);
};
//用于指定连接关闭后的回调函数。
ws.onclose = function () {
console.log(“WebSocket instance closed.”);
};

    setTimeout(function () {
        //当前的链接状态
        if (ws.readyState === WebSocket.OPEN) {
            ws.send("Hello WebSocket, I'm a text message.");
        } else {
            console.log("WebSocket instance wasn't ready...");
        }
    }, 10);

谢谢

后端呢?11

找到问题了,go做的后端需要额外处理一下,谢谢哈

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。