我如何开始 Socket.IO
How do i start Socket.IO
我想在索引上添加 socket.io 就像这样我需要弄清楚如何在此处使用此代码执行此操作并且我想在另一个文件中调用路由时发出数据我怎样才能做到这一点?你可以看到我试过代码来放置套接字 io 但我不知道有人可以帮忙吗?这也是在后端制作的,就像这应该是一个 API,我不会有前端,这是我的问题,我从来没有像这样使用 socket.io
// all the requires
require('./models/Service');
require('./models/Activities');
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const serviceRoutes = require('./routes/serviceRoutes');
const activityRoutes = require('./routes/activitiesRoutes');
const errorHandler = require('./helpers/Error-handler');
const logger = require('./config/winston');
const http = require('http').Server(app);
// all the app use
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(serviceRoutes);
app.use(activityRoutes);
app.use(errorHandler);
// this calls for the users route to authenticate
app.use('/users', require('./Users/user.controller'));
// connection to database
mongoose.connection.on('connected', () => {
console.log('Connected to mongo instance');
});
mongoose.connection.on('error', err => {
console.error('Error connecting to mongo', err);
});
// server start up
const port = process.env.NODE_ENV === 'production' ? 80 : 4000;
http.listen(port, function() {
console.log('listening on ' + port);
try {
logger.info('Server and Database is initiated');
}
catch (error) {
logger.error(error);
}
});
// implementation of io
const io = require("socket.io")(http);
io.on('connection', function(socket) {
console.log('A user connected');
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
app.get('/', function(req, res) {
res.send(console.log('hey', io))
});
module.exports = io;
您可以让套接字加入 room 连接,并使用 app.set('io', io)
将 io
附加到应用程序对象。然后可以使用 req.app.get('io')
在控制器中访问它,并且您可以向特定房间发射。
我想在索引上添加 socket.io 就像这样我需要弄清楚如何在此处使用此代码执行此操作并且我想在另一个文件中调用路由时发出数据我怎样才能做到这一点?你可以看到我试过代码来放置套接字 io 但我不知道有人可以帮忙吗?这也是在后端制作的,就像这应该是一个 API,我不会有前端,这是我的问题,我从来没有像这样使用 socket.io
// all the requires
require('./models/Service');
require('./models/Activities');
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const serviceRoutes = require('./routes/serviceRoutes');
const activityRoutes = require('./routes/activitiesRoutes');
const errorHandler = require('./helpers/Error-handler');
const logger = require('./config/winston');
const http = require('http').Server(app);
// all the app use
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(serviceRoutes);
app.use(activityRoutes);
app.use(errorHandler);
// this calls for the users route to authenticate
app.use('/users', require('./Users/user.controller'));
// connection to database
mongoose.connection.on('connected', () => {
console.log('Connected to mongo instance');
});
mongoose.connection.on('error', err => {
console.error('Error connecting to mongo', err);
});
// server start up
const port = process.env.NODE_ENV === 'production' ? 80 : 4000;
http.listen(port, function() {
console.log('listening on ' + port);
try {
logger.info('Server and Database is initiated');
}
catch (error) {
logger.error(error);
}
});
// implementation of io
const io = require("socket.io")(http);
io.on('connection', function(socket) {
console.log('A user connected');
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
app.get('/', function(req, res) {
res.send(console.log('hey', io))
});
module.exports = io;
您可以让套接字加入 room 连接,并使用 app.set('io', io)
将 io
附加到应用程序对象。然后可以使用 req.app.get('io')
在控制器中访问它,并且您可以向特定房间发射。