首页 > 解决方案 > 如何使用 Cloud Functions 实现 socket.IO?

问题描述

所以基本上我在做一个游戏,服务器向客户端发送消息,首先回答的客户端收到 1 pnt。我正在尝试创建房间以改善多人游戏模式,但我被困在这一点上。

我正在尝试将 socket.io 连接到我的 google 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 } });
});

该功能是否仅因为我的 Firebase 计划受到限制而崩溃?还是有其他问题?

标签: node.jsfirebasesocket.iogoogle-cloud-functions

解决方案


无法将 Cloud Functions 用作基于套接字的 I/O 的主机。每次在任何端口上调用“监听”都会失败。提供的网络基础设施仅处理单个 HTTP 请求,每个请求的请求和响应负载大小为 10MB。您无法控制它如何在网络级别处理请求和响应。


推荐阅读