最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何使用Cloud Functions实现socket.IO?

网站源码admin18浏览0评论

如何使用Cloud Functions实现socket.IO?

如何使用Cloud Functions实现socket.IO?

所以基本上,我正在做一个游戏,服务器将消息发送给客户端,而首先应答的客户端将收到1分。我正在尝试创建用于改进多人游戏模式的房间,但此时此刻,我陷入了困境。

我正在尝试将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, "");
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计划受到限制而崩溃?还是还有其他问题?

回答如下:

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

发布评论

评论列表(0)

  1. 暂无评论