如何实现套接字.IO与云功能



所以基本上我在做一个游戏,服务器向客户端发送消息,第一个回答的客户端收到1 pnt。我正试图创建房间来改进多人游戏模式,但我在这一点上陷入了困境。

我正试图将socket.io连接到我的谷歌Firebase函数,但当我调用该函数时,它会返回以下错误:

Billing account not configured. External network is not accessible and quotas are severely limited. 
Configure billing account to remove these restrictions
10:13:08.239 AM
addStanza
Uncaught exception
10:13:08.242 AM
addStanza
Error: getaddrinfo EAI_AGAIN at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)
10:13:08.584 AM
addStanza
Error: function crashed out of request scope Function invocation was interrupted.

这是代码:

//firebase deploy --only functions
const Proverbi = require('./Proverbi.js');
const socketIo = require("socket.io");
const https = require("https");
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var server = https.createServer();
server.listen(443, "https://us-central1-chip-chop.cloudfunctions.net");
var io = socketIo.listen(server);
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addStanza = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const nome = req.query.nome;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/stanze').push({ giocatori: { giocatore: { nome: nome, punteggio: 0 } } });
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
//res.redirect(200, nome.toString());
var link = snapshot.toString().split('/');
res.json({ idStanza: link[4] });
});
// Listens for new messages added to /messages/:pushId/original and creates an
//  uppercase version of the message to /messages/:pushId/uppercase
exports.addFirstPlayer = functions.database.ref('/stanze/{pushId}/giocatori/giocatore/nome')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const nome = snapshot.val();
// const snapshot3 = snapshot.ref('/stanza/{pushId}/giocatori/giocatore').remove();
const snapshot2 = snapshot.ref.parent.parent.remove();
var room = snapshot.ref.parent.parent.parent.val();
// handle incoming connections from clients
io.sockets.on('connection', function (socket) {
// once a client has connected, we expect to get a ping from them saying what room they want to join
socket.on('room', function (room) {
socket.join(room);
});
});
io.sockets.in(room).emit('message', nome + 'Si è unito alla stanza');
return snapshot.ref.parent.parent.push({ nome: nome, punteggio: 0, room:room });
});
exports.addPlayer = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const nome = req.query.nome;
const idStanza = req.query.id;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/stanze/' + idStanza + "/giocatori").push({ nome: nome, punteggio: 0 });
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
var room = idStanza;
// handle incoming connections from clients
io.sockets.on('connection', function (socket) {
// once a client has connected, we expect to get a ping from them saying what room they want to join
socket.on('room', function (room) {
socket.join(room);
});
});
io.sockets.in(room).emit('message', nome + 'Si è unito alla stanza');
//res.redirect(200, nome.toString());
res.json({ success: { id: idStanza } });
});

功能崩溃只是因为我的防火基地计划有限吗?或者还有其他问题吗?

不可能将云函数用作基于套接字的I/O的主机。每次在任何端口上调用"侦听"都会失败。所提供的网络基础结构仅处理单个HTTP请求,每个请求的请求和响应有效负载大小为10MB。您无法控制它如何在网络级别处理请求和响应。

最新更新