Я реализую веб-приложение, используя Vue. В приложении я использую «толкатель» для создания многопользовательского общения в реальном времени. В частности, я настроил сервер node.js, прослушивающий порт 5000 определенного устройства c в моей локальной сети. Приложение работает в моей домашней сети (я могу взаимодействовать с различными подключенными устройствами). Теперь я хочу развернуть приложение на Firebase.
Ниже приведен код, связанный с определением экземпляра отправителя, со свойством «authEndpoint», которое указывает путь, по которому пользователь должен отправить HTTP-запрос на публикацию, чтобы подписаться на общий канал. Когда я развертываю приложение на firebase, как мне следует управлять свойством authEndpoint и положением сервера node.js, чтобы приложение работало на inte rnet?
import Pusher from 'pusher-js'
const pusher = new Pusher('*************', {
cluster: 'eu',
encrypted: true,
authEndpoint: '192.168.1.237:5000/pusher/auth'
})
Это код, связанный с сервером:
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const app = express()
const Pusher = require('pusher')
const crypto = require('crypto')
const cors = require('cors')
//initialize Pusher with your appId, key and secret
const pusher = new Pusher({
appId: '****',
key: '**************',
secret: '************',
cluster: 'eu',
encrypted: true
})
// Body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors())
// The code below helps to fix any potential CORS issue.
app.use((req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*')
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true)
// Pass to next layer of middleware
next()
})
// Index API route for the Express app
app.get('/', (req, res) => {
res.send('Welcome')
})
// API route used by Pusher as a way of authenticating users
app.post('/pusher/auth', (req, res) => {
let socketId = req.body.socket_id
let channel = req.body.channel_name
// Generate a random string and use as presence channel user_id
let presenceData = {
user_id: crypto.randomBytes(16).toString('hex')
}
let auth = pusher.authenticate(socketId, channel, presenceData)
res.send(auth)
})
// Set port to be used by Node.js
app.set('port', (5000))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})