如何将socket.io附加到Google Firebase应用程序功能? [英] How to attach socket.io to google firebase app functions?

查看:75
本文介绍了如何将socket.io附加到Google Firebase应用程序功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在index.js文件中,我的以下代码位于我的Google Firebase proyect中的functions文件夹中:

I have the following code in index.js file located into functions folder in my google firebase proyect:

net=require('express')()
net.get('/',function(req,res){res.sendFile(__dirname+'/slave.htm')})
exports.run=require('firebase-functions').https.onRequest(net)
require('socket.io').listen(net).on("connection",function(socket){})

但是当我在命令提示符下执行 \ gfp1> firebase deploy 时,这会给我带来错误:

But when I execute \gfp1>firebase deploy in command prompt, this give me that errors:

您正在尝试将socket.io附加到快速请求处理程序函数.请传递一个http.Server实例.

You are trying to attach socket.io to an express request handler function. Please, pass a http.Server instance.

是的,我通过以下代码传递和http服务器实例:

Yes, and I pass and http server instance in the following code:

net=require('firebase-functions').https.onRequest((req,res)=>{res.send("socket.io working!")})
exports.run=net;require('socket.io').listen(net).on("connection",function(socket){})

它再次给我以下错误:

您正在尝试将socket.io附加到快速请求处理程序函数.请传递一个http.Server实例.

You are trying to attach socket.io to an express request handler function. Please, pass a http.Server instance.

然后我尝试使用该代码将socket.io附加到firebase函数:

And I try attaching socket.io to firebase functions with that code:

net=https.onRequest((req,res)=>{res.send("socket.io working!")})
exports.run=require('firebase-functions').net
require('socket.io').listen(require('firebase-functions').net).on("connection",function(socket){})

这会导致此错误:

未定义https

https is not defined

当我在localhost中运行此代码时:

When I run this code in localhost:

app=require('express')()
app.get('/',function(req,res){res.sendFile(__dirname+'/slave.htm')})
net=require('http').createServer(app);net.listen(8888,function(){console.log("Server listening.")})
require('socket.io').listen(net).on("connection",function(socket){})

控制台发出 \ gfp>服务器侦听.,并且当我转到url http://127.0.0.1:8888 时,它可以将html文件发送到导航器,正如我期望的那样:

The console emmit \gfp>Server listening., and when I go to url http://127.0.0.1:8888, it works sending an html file to navigator, as I expected:

<script>
document.write("File system working!")
document.body.style.backgroundColor="black"
document.body.style.color="white"
</script>

但是当我尝试转换 net = require('http').createServer(app); net.listen(8888,function(){console.log(服务器监听".)}}时,就会发生问题) net = exports.run = require('firebase-functions').https.onRequest((req,res)=> {res.send("Firebase正常!")}),这似乎是不可能的.

But the problem happens when I try to convert net=require('http').createServer(app);net.listen(8888,function(){console.log("Server listening.")}) to net=exports.run=require('firebase-functions').https.onRequest((req,res)=>{res.send("Firebase working!")}), it seems to be impossible.

推荐答案

您无法运行代码以使用Cloud Functions在某些端口上进行侦听.这是因为您不能保证只有一台机器或实例在运行您的代码.它可以分布在许多同时运行的实例之间.您不必知道或不在乎这种情况-云功能可以扩展以满足您功能上的需求.

You can't run code to listen on some port with Cloud Functions. This is because you aren't guaranteed to have a single machine or instance running your code. It could be distributed among many instances all running concurrently. You shouldn't know or care if that happens - Cloud Functions will just scale to meet the needs placed on your functions.

如果部署HTTP类型的功能,它将自动在https端口上侦听项目专用主机,然后您可以向其发送Web请求.

If you deploy an HTTP type function, it will automatically listen on the https port for the dedicated host for your project, and you can send web requests to that.

如果要通过持久保持的套接字执行事务,请使用数据库中的实时数据库客户端写值,然后使用您编写的数据库触发函数来响应这些写操作.该功能可以通过在客户端正在侦听的位置将一些内容写回数据库,从而将数据发送回客户端.

If you want to perform transactions over a persistently held socket, use the realtime database client write values in the database, then respond to those writes with a database trigger function that you write. That function can send data back to the client by writing something back to the database in a location that's being listened to by the client.

这篇关于如何将socket.io附加到Google Firebase应用程序功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆