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

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

问题描述

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

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).

一切都表明节点部分运行良好.但是聊天不起作用,表明 socket.io 部分不起作用.您可以在此处查看部署的应用(和页面源).

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.

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

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

yaml 内容:

runtime: nodejs
vm: true

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

json 内容:

{
  "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.

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

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.

如果是 socket.io,请在服务器端执行以下步骤.下面的代码片段.

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

  1. 创建第二个快速中间件和服务器.
  2. 在新服务器上附加/使用 socket.io.
  3. 监听端口 (65080).
  4. 为 google 计算引擎上的端口 (65080) 打开防火墙.
  5. 链接到工作代码库.

<小时>

socket.io 在服务器端的变化

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

<小时>

通过命令打开防火墙

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

<小时>

我希望 Google 尽快提出生产就绪的解决方案,因为这将成为任何 PaaS 武器库中的关键盔甲.


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天全站免登陆