我使用 socket.io 创建了我的第一个 node.js 应用程序。具体来说,我实现了 chat example由 socket.io 发布。它在本地完美运行。然后我尝试将其部署到 Google App Engine(对 Node 进行一些代码调整以使其正常工作)。
一切都表明 Node 部分运行良好。但是聊天不起作用,表明 socket.io 部分不起作用。您可以看到部署的应用程序(和页面源)here .
我还需要做任何额外的事情吗? yaml 或 json 文件中的内容?
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"
}
}
最佳答案
简而言之,这不能在生产中完成,它似乎是 work in process .正确的架构是在谷歌计算引擎上拥有一个聊天服务器,如 here 所述.
但作为概念证明,在谷歌应用引擎上使用 socket.io 与 google appengine samples for websockets 中显示的非常相似。 .
如果是 socket.io,请在服务器端执行以下步骤。下面的代码片段。
- 创建第二个快速中间件和服务器。
- 在新服务器上附加/使用 socket.io。
- 监听端口 (65080)。
- 为谷歌计算引擎上的端口 (65080) 打开防火墙。
- 链接到 working repository .
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 武器库的关键装甲。
关于node.js - 如何将 socket.io 部署到 Google App Engine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401250/