如何将socket.io部署到Google App Engine? [英] How do I deploy socket.io to Google App Engine?

查看:106
本文介绍了如何将socket.io部署到Google App Engine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用socket.io创建了我的第一个node.js应用程序。具体来说,我实现了由socket.io发布的聊天示例。它在当地完美运作。然后我尝试将它部署到Google App Engine(使一些代码调整为节点工作)。

所有内容都显示表明节点部分运行良好。但聊天不起作用,表明socket.io部分不起作用。您可以在此处查看部署的应用程序(和页面源代码)。



我需要额外做些什么吗? yaml或json文件中的某些内容?



yaml content:

  runtime:nodejs 
vm:true

skip_files:
- ^(。* /)?。* / node_modules /.*$

json content:

<$ {
name:Chaty,
description:chatrooms app,
version:0.0.1,
private:true,
license:Apache Version 2.0,
author:McChatface,
engines:{
node:〜4.2
},
scripts:{
start:node app.js,
monitor:nodemon app.js ,
deploy:gcloud preview app deploy
},
dependencies:{
express:^ 4.13.4,
socket.io:^ 1.4.6
}
}


解决方案

总之,这不能在生产中完成,它似乎是在制品。正确的架构是在Google计算引擎上有一个聊天服务器,如此处

但是,在谷歌应用引擎上使用socket.io的概念证明与 websockets的google appengine示例

对于socket.io,请在服务器端执行以下步骤。下面的代码片段。
$ b


  1. 创建第二个快递中间件和服务器。

  2. 在新服务器上附加/使用socket.io。
  3. 收听端口(65080)。
  4. 打开Goog​​le计算引擎上端口(65080)的防火墙。

  5. 链接到工作存储库
  6. >






服务器端的socket.io更改

  var app_chat = require('express')(); 
var server1 = require('http')。Server(app_chat);
var io = require('socket.io')(server1);
server1.listen(65080);
$ b $ io.on('connection',function(socket){
console.log('user connected');
socket.on('chat_message',function(data ){
console.log('client sent:',data);
socket.emit('chat_message','服务器正在回应您的消息:'+ data);
});
});






通过命令打开防火墙

  gcloud compute防火墙规则创建默认允许websockets \ 
--allow tcp:65080 \
--target-tags websocket \
--description允许端口65080上的websocket流量






我希望Google能够尽快推出可立即投入使用的解决方案,因为这将成为任何PaaS-arsenal的关键装甲。


I created my first node.js app using socket.io. Specifically I implemented the chat example published by socket.io. It works perfectly, locally. And then I tried deploying it to Google App Engine (making some code tweaks for node to work).

Everything shows up indicating that the node part is working well. However the chat doesn't work indicating that socket.io part isn't working. You can see the deployed app (and page source) here.

Do I have to do anything additional? Something in the yaml or json files?

yaml content:

runtime: nodejs
vm: true

skip_files:
  - ^(.*/)?.*/node_modules/.*$

json content:

{
  "name": "Chaty",
  "description": "chatrooms app",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "McChatface",
  "engines": {
    "node": "~4.2"
  },
  "scripts": {
    "start": "node app.js",
    "monitor": "nodemon app.js",
    "deploy": "gcloud preview app deploy"
  },
  "dependencies": {
    "express": "^4.13.4",
    "socket.io": "^1.4.6"
  }
}

解决方案

In short this cannot be done on production and it appears to be work in process. The right architecture is to have a chat server on google compute engine as outlined here.

But as a proof of concept to use socket.io on google app engine is very similar to that shown in google appengine samples for websockets.

In case of socket.io do the following steps on server side. Code snippet below.

  1. Create second express middleware and server.
  2. Attach/use socket.io with new server.
  3. Listen to port (65080).
  4. Open firewall for port (65080) on google compute engine.
  5. Link to working repository.


socket.io changes on server side

    var app_chat = require('express')();
    var server1 = require('http').Server(app_chat);
    var io = require('socket.io')(server1);
    server1.listen(65080);

    io.on('connection', function (socket) {
      console.log('user connected');
      socket.on('chat_message', function (data) {
        console.log('client sent:',data);
        socket.emit('chat_message', 'Server is echoing your message: ' + data);
      });
    });


open firewall by command

gcloud compute firewall-rules create default-allow-websockets \
    --allow tcp:65080 \
    --target-tags websocket \
    --description "Allow websocket traffic on port 65080"


I hope Google comes up with a production-ready solution soon enough on as this will become a key armour in any PaaS-arsenal.

这篇关于如何将socket.io部署到Google App Engine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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