我不确定我错过了什么,可以让这款应用在Google Cloud上正常工作 [英] I'm not sure what I'm missing to make this app work correctly on Google Cloud when it comes to Websockets

查看:320
本文介绍了我不确定我错过了什么,可以让这款应用在Google Cloud上正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Google Cloud部署了一个实时绘图应用程序,在这个应用程序中,多个用户可以看到其他人吸引并加入。



我的代码一直存在的问题是这部分:

  var socket = io.connect(http://bla-bla-1234.appspot.com:8080); 

如果地址是这样的,我经常会在控制台显示错误信息,如 WebSocket错误:错误的HTTP响应。状态代码400,错误请求在IE或上进行测试时Firefox无法在wss://bla-bla-1234.appspot.com/socket上建立与服务器的连接.io /?EIO = 3& transport = websocket& sid = ULP5ZcoQpu0Y_aX6AAAB。在Firefox上进行测试时。

code> var socket = io.connect(); 这么多只是为了看看是否可以看到一些实时绘图,在某些情况下我已经能够但不平滑绘图,其中多行会在一个用户的屏幕上出现,当你所做的只是画一条线。并且经常会遇到以下错误:与'//bla-bla-1234.appspot.com/socket.io/?EIO=3&transport=websocket&sid=dJqZc2ZutPuqU31HAAX'的WebSocket连接失败:在WebSocket握手过程中出错:意外的响应代码: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:网络错误0x2efd,由于错误00002efd,无法完成操作。 

我猜我的问题是,我该如何去确定参数中的正确地址并在预期(活)平稳工作?



预先致谢。

解决方案我接触了支持,因为我有同样的问题。



答案很简短:Google App Engine的托管虚拟机目前不支持WebSockets 。

在实现该功能之前,唯一可能的方法是强制Socket.io使用长轮询。



他们告诉了我。但我没有尝试。就我而言,我会尝试切换到Google Compute Engine。



编辑:我还有其他来自Google的信息。让我引用@ jmdobry的回答:


目前,只有 ws:// 协议被支持(而不是 wss:// ),并且为了使用websocket,您需要获取运行应用的计算机的外部IP地址。不幸的是,socket.io在他们的实现中做了一些还没有工作的东西,尽管我们有一个手动使用websockets并访问元数据来获取外部ip地址的例子: 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


I have deployed a real-time drawing application to Google Cloud, where multiple users can see others draw and join in too.

The problem I have been having with my code is this part:

var socket = io.connect("http://bla-bla-1234.appspot.com:8080");

When the address is left like this, I often get errors displayed to the console such as WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request when testing on IE or 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. when testing on Firefox.

I have changed the parameters of var socket = io.connect(); so much just to see if I can see some live drawing, in some cases I have been able to but not smooth drawing, Where multiple lines will come up on the screen by one user, when all you're doing is drawing the line once. And often come up with errors such as: 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.

Here is what allows the connection to server and what allows the data to be displayed to all clients connected on the client.js file, this is not all the code but I felt this part was the most relevant to this issue:

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();
    });

I have tried to add a port (8080) within the parameter but only to receive an error such as this:

XMLHttpRequest: Network Error 0x2efd, Could not complete the operation due to error 00002efd.

I guess my guess my question is, how do I go about figuring out the right address within the parameters and have it work as in intended (live) smoothly?

Thanks in advance.

解决方案

I contacted the support because I had the same issue.

The answer is short: Google App Engine's Managed VM don't support WebSockets at the moment.

Until that feature is implemented, the only way to possibly make it work is to force Socket.io to use long-polling.

They told me that. But I didn't try. In my case, I'll try to switch to Google Compute Engine instead.

Edit: I've got other info from Google. Let me quote @jmdobry's answer :

Currently, only the ws:// protocol is supported ( not wss:// ), 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

Edit 2: I managed to make my app work on Google Compute Engine. It's a bit more complicated and time consuming, and to be honest, I didn't understand 100% of the steps. But I followed this section of the tutorial (with some minor changes like the project name in all the files), and it went well: https://cloud.google.com/nodejs/getting-started/run-on-compute-engine

这篇关于我不确定我错过了什么,可以让这款应用在Google Cloud上正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆