我已经在 Google Cloud 上部署了一个实时绘图应用程序,多个用户可以在其中看到其他人的绘图并加入其中。
我的代码一直存在的问题是这部分:
var socket = io.connect("http://bla-bla-1234.appspot.com:8080");
本地址像这样留下时,我经常会在控制台上显示错误,例如 WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request
在 IE 或 Firefox can't establish a connection to the server at wss://bla-bla-1234.appspot.com/socket.io/?EIO=3&transport=websocket&sid=ULP5ZcoQpu0Y_aX6AAAB.
上测试时在 Firefox 上测试时。
我已经更改了 var socket = io.connect();
的参数这么多只是为了看看我是否能看到一些实时绘图,在某些情况下我已经能够但不是流畅的绘图,一个用户会在屏幕上出现多条线,而你所做的只是画一次线.并且经常会出现这样的错误:Websocket connection to '//bla-bla-1234.appspot.com/socket.io/?EIO=3&transport=websocket&sid=dJqZc2ZutPuqU31HAAX' failed: Error during WebSocket handshake: Unexpected response code: 400
.
这是允许连接到服务器以及允许数据显示给 client.js 文件上连接的所有客户端的部分,这不是所有代码,但我认为这部分与此问题最相关:
var socket = io.connect("bla-bla-1234.appspot.com");
socket.on('draw_line', function (data) {
var line = data.line;
context.beginPath();
context.strokeStyle = "#33ccff";
context.lineJoin = "round";
context.lineWidth = 5;
context.moveTo(line[0].x, line[0].y);
context.lineTo(line[1].x, line[1].y);
context.stroke();
});
我试图在参数中添加一个端口 (8080),但只收到如下错误:
XMLHttpRequest: Network Error 0x2efd, Could not complete the operation due to error 00002efd.
我想我的问题是,如何在参数中找出正确的地址并使其按预期(实时)顺利运行?
提前致谢。
最佳答案
我联系了支持人员,因为我遇到了同样的问题。
答案很简短:Google App Engine 的托管虚拟机目前不支持 WebSockets。
在实现该功能之前,唯一可能使其起作用的方法是强制 Socket.io 使用长轮询。
他们告诉我的。但我没有尝试。就我而言,我会尝试改用 Google Compute Engine。
编辑:我从 Google 获得了其他信息。让我引用@jmdobry 的回答:
Currently, only the
ws://
protocol is supported ( notwss://
), and in order to use websockets you need to get the external ip address of the machine your app is running on. Unfortunately, socket.io does something in their implement that doesn’t work yet, though we have an example of using websockets manually and accessing the metadata to get the external ip address: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/websockets
编辑 2:我设法让我的应用程序在 Google Compute Engine 上运行。有点复杂,比较费时间,说实话,我100%的步骤都没有看懂。但是我遵循了教程的这一部分(有一些小的变化,比如所有文件中的项目名称),并且进展顺利:https://cloud.google.com/nodejs/getting-started/run-on-compute-engine
关于javascript - 当涉及到 Websockets 时,我不确定要使此应用程序在 Google Cloud 上正常运行我缺少什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116039/