【问题标题】:Problems connecting to socket.io using firebase functions and express使用 firebase 函数和 express 连接到 socket.io 的问题
【发布时间】:2020-05-03 06:13:25
【问题描述】:

我正在尝试将 socket.io 连接与 firebase 托管/功能一起使用,但我遇到了一些问题。它从 CORS 问题开始(我确定这仍然是问题),但现在我完全迷失了,下面是我的 firebase.json、index.js(firebase 函数文件),甚至是 angular 客户端文件初始化与服务器的连接。

firebase.json

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "headers": [ 

      {
        "source" : "**",
        "headers" : [ {
          "key" : "Access-Control-Allow-Headers",
          "value" : "Origin"
        } ]
      },
      {
        "source" : "**",
        "headers" : [ {
          "key" : "Access-Control-Allow-Origin",
          "value" : "http://localhost:4200"
        } ]
      }
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  }
}

index.js

const functions = require('firebase-functions');
const express = require('express');
var app = express();

const http = require('http').Server(express);
const socketio = require('socket.io')(http);
const cors = require('cors');


app.use(cors({credentials: true, origin: '*:*'}));
app.get('/tester', (request, response) => {

    //response.header('Access-Control-Allow-Origin', '*');
    response.send('Hello!');

    socketio.on('connection', socket => {
        connections.push(socket);
        console.log('New client connected ('+connections.length+' connections).');
        //console.log(socket);
        socket.emit('port', 'LIVE SHIT');
    });
})

exports.app = functions.https.onRequest(app)

app.component.ts(用于连接的客户端组件)

import { Component } from '@angular/core';
import io from 'socket.io-client';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'multi-client';
  port: number = 0;
  socket: any;
  width: number = 100;
  updateVal: number = 5;
  constructor()
  {
    this.socket = io('http://localhost:5000/tester');

    this.socket.on('port', port => {
      this.port = port;
    })
  }

  updateWidth(val)
  {
    this.width += val;
  }
}

这是我遇到的错误。

我在 stackoverflow 和其他各种网站上查看了一些帖子,这些帖子有类似的问题,但似乎没有任何效果。我确定我做错了,但我迷失了实现这一目标所缺少的东西。请帮忙!

【问题讨论】:

    标签: node.js firebase express google-cloud-functions firebase-hosting


    【解决方案1】:

    Cloud Functions 适用于相对短暂且具有明确目的的操作。您不能使用 Cloud Functions 保持与客户端的连接打开。

    原因是 Cloud Functions 会在它认为您完成时关闭容器的资源。对于像你这样的 HTTP 函数,这意味着当你调用 response.send('Hello!'); 时,这些资源就消失了。

    所以可能的是,一旦您建立了这样的套接字连接,就可以将响应发送到客户端:

    app.get('/tester', (request, response) => {
    
    
        socketio.on('connection', socket => {
            connections.push(socket);
            console.log('New client connected ('+connections.length+' connections).');
            //console.log(socket);
            socket.emit('port', 'LIVE SHIT');
    
            response.send('Hello!');
        });
    })
    

    但在这种情况下,在调用response.send('Hello!'); 后连接也会关闭。虽然您可以发送响应,但即使在这种情况下,连接/资源也会在 9 分钟后关闭,因为这是云函数可以运行的最长时间。

    这一切都可以追溯到我最初的声明,即 Cloud Functions 仅适用于具有明确结束时刻的短期操作。对于长时间运行的流程,请使用其他平台,例如 App Engine、Compute Engine 或您可以管理自己的流程的众多其他产品之一。

    另见:

    【讨论】:

    • 哦,我明白了,我害怕像 thius 这样的东西。应用引擎会是实现这一目标的免费方式吗?你能给我一个链接吗?
    • 任何可以托管长时间运行的套接字客户端的平台都可以工作。谷歌搜索应该会给你很多选择,我链接的问题也是如此。
    • 谢谢!我现在正在前进。
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2018-10-13
    相关资源
    最近更新 更多