在谷歌应用程序引擎节点应用程序中的套接字io [英] Socket io in node app on google app engine

查看:171
本文介绍了在谷歌应用程序引擎节点应用程序中的套接字io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在GAE上运行使用套接字io在节点js中编写的p2p聊天。



我的应用程序在本地工作正常,但运行时收到错误消息它在GAE服务器上与我认为的socket io相关。



以下是我本地 client.html 在本地运行时:

 < script src =/ socket.io/socket.io.js\"> ;< /脚本> 
<! - < script src =https://cdn.socket.io/socket.io-1.2.0.js>< / script> - >
< script type =text / javascript>
//我们的socket.io代码在这里
var socketio = io.connect(127.0.0.1:1337);
socketio.on(message_to_client,function(data){
to_history(data ['message']);
});

函数send_message(){
var msg = [document.getElementById(text1)。value,user1,uuid];

socketio.emit(message_to_server,{
message:msg
});
}

< / script>

我见过博客/文章说,为了部署,我需要允许防火墙规则这里的SO (现在已经到位)。我也尝试将我部署的应用程序指向一个静态外部IP,例如(在我的谷歌云控制台中使其静态):

  var socketio = io.connect('https://104.197.51.XXX')

或指向它到我的防火墙规则中指定的端口65080(请参阅google 这里

  var socketio = io.connect('https://104.197.51.XXX:65080 ')

这些都无效。

我已经装好了html和jQuery的一部分,而且css也加载得很好,它只是套接字的东西,我似乎错了,我必须改变什么?



如果这是有用的,这里是app.yaml:

  runtime:nodejs 
vm :true

非常感谢任何帮助,谢谢。

解决方案

不幸的是,App Engine不支持websockets(还)。你使用的黑客真的不可靠的原因有几个:


  • 它直接连接到实例,它可以去随时关闭或者随时回收

  • 缺少魔法哈希,真的没有办法让https沿着这条路线走下去。



总之 - 这不是生产准备。那已经说过了......



https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/websockets



该链接将向您展示如何设置它的一个工作示例和说明。



相反,我建议使用pubnub:
https://www.pubnub.com/docs/nodejs-javascript/pubnub-javascript-sdk



它有一个非常好的API,它将比任何可以与App Engine一起攻击的任何东西都更加可靠(你知道的,直到我们解决这个问题)。你可以看到我在这里完成的几个例子:



我希望这有助于您!


I'd like to run a p2p chat written in node js with socket io on GAE.

My app works locally fine but I get error messages when I run it on the GAE servers related to the socket io I think.

Here are the two relevant script tags of my local client.html when running locally:

<script src="/socket.io/socket.io.js"></script>
<!-- <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script> -->
<script type="text/javascript">
    // our socket.io code goes here
    var socketio = io.connect("127.0.0.1:1337");
    socketio.on("message_to_client", function (data) {
        to_history(data['message']);
    });

    function send_message() {
        var msg = [document.getElementById("text1").value, user1, uuid];

        socketio.emit("message_to_server", {
            message: msg
        });
    }

</script>

I've seen blogs/posts saying that for deployment I need to allow a firewall rule here on SO (which is in place now). I also tried pointing my deployed app to a static external IP like (after making it static in my google cloud console):

var socketio = io.connect('https://104.197.51.XXX')

or to point it to the port 65080 specified in my firewall rule (see documentation by google here:

var socketio = io.connect('https://104.197.51.XXX:65080')

None of this works.

I have the html loaded fine and the jQuery part I have and css is also loading just fine. It's just the socket stuff that I seem to be getting wrong. What do I have to change?

If this is of use, here the app.yaml:

runtime: nodejs
vm: true

Any help is greatly appreciated. Thanks.

解决方案

Sadly, App Engine just doesn't support websockets (yet). The hack-around you're using is really unreliable for a few reasons:

  • it makes a direct connection to the instance, which can go down or be recycled at any time
  • Short of magic hackery, there's really no way to get https going down this route.

In short - this is not production ready. That having been said....

https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/websockets

That link will show you a working example and instructions of how to set it up.

Instead of that - I'd suggest using pubnub: https://www.pubnub.com/docs/nodejs-javascript/pubnub-javascript-sdk

It has a really nice API, and is going to be way more reliable than anything you can hack together with App Engine (you know, until we fix this). You can see a few examples that I've done here:

I hope this helps!

这篇关于在谷歌应用程序引擎节点应用程序中的套接字io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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