我正在尝试将 socket.io 与 Strapi 集成。但不幸的是,如果没有涵盖这方面的任何适当的教程或文档,我一直无法做到这一点。
我跟着我在网上找到的唯一资源是:https : //medium.com/strapi/strapi-socket-io-a9c856e915a6 但我认为这篇文章已经过时了。我似乎无法在不遇到大量错误的情况下运行其中提到的代码。
下面是我实现它的尝试,我一直在尝试通过 chrome websocket 插件智能 websocket 客户端连接它但是当我尝试运行服务器时我没有得到任何响应。
我完全在黑暗中。任何帮助将不胜感激
module.exports = ()=> {
// import socket io
var io = require('socket.io')(strapi.server)
console.log(strapi.server) //undefined
// listen for user connection
io.on('connect', socket => {
socket.send('Hello!');
console.log("idit")
// or with emit() and custom event names
socket.emit('greetings', 'Hey!', { 'ms': 'jane' }, Buffer.from([4, 3, 3, 1]));
// handle the event sent with socket.send()
socket.on('message', (data) => {
console.log(data);
});
// handle the event sent with socket.emit()
socket.on('salutations', (elem1, elem2, elem3) => {
console.log(elem1, elem2, elem3);
});
});
};
Run Code Online (Sandbox Code Playgroud)
Moi*_*ail 13
所以我找到了解决方案。好极了。我会把它放在这里以防万一有人需要它。
boostrap.js
module.exports = async () => {
process.nextTick(() =>{
var io = require('socket.io')(strapi.server);
io.on('connection', async function(socket) {
console.log(`a user connected`)
// send message on user connection
socket.emit('hello', JSON.stringify({message: await strapi.services.profile.update({"posted_by"})}));
// listen for user diconnect
socket.on('disconnect', () =>{
console.log('a user disconnected')
});
});
strapi.io = io; // register socket io inside strapi main object to use it globally anywhere
})
};
Run Code Online (Sandbox Code Playgroud)
在以下位置找到:https : //github.com/strapi/strapi/issues/5869#issuecomment-619508153 _
显然, socket.server 在服务器启动时不可用。因此,您必须使用等待 socket.server 初始化的 process.nextTick。
我还将添加一些我在设置时遇到的问题。
我如何从外部客户端(如 nuxt、vue 或 react)连接?
您只需要通过“http://localhost:1337”进行连接,这是我常用的 Strapi 地址。
我使用 nuxt 作为我的客户端,这就是如何在客户端设置我的 socketio
modules:[
...
'nuxt-socket-io',
...
],
io: {
// module options
sockets: [
{
name: 'main',
url: 'http://localhost:1337',
},
],
},
Run Code Online (Sandbox Code Playgroud)
created() {
this.socket = this.$nuxtSocket({})
this.socket.on('hello', (msg, cb) => {
console.log('SOCKET HI')
console.log(msg)
})
},
Run Code Online (Sandbox Code Playgroud)
它有效。
将第三方服务集成到 Strapi 的一种简洁方法是使用hooks。它们在服务器启动期间加载一次。在本例中,我们将创建一个本地钩子。
以下示例已与strapi@3.6.
socket.io为at创建一个钩子./hooks/socket.io/index.jsmodule.exports = strapi => {
return {
async initialize() {
const ioServer = require('socket.io')(strapi.server, {
cors: {
origin: process.env['FRONT_APP_URL'],
methods: ['GET', 'POST'],
/* ...other cors options */
}
})
ioServer.on('connection', function(socket) {
socket.emit('hello', `Welcome ${socket.id}`)
})
/* HANDLE CLIENT SOCKET LOGIC HERE */
// store the server.io instance to global var to use elsewhere
strapi.services.ioServer = ioServer
},
}
}
Run Code Online (Sandbox Code Playgroud)
./config/hook.jsmodule.exports = {
settings: {
'socket.io': {
enabled: true,
},
},
};
Run Code Online (Sandbox Code Playgroud)
这样就完成了。您可以访问内部的 websocket 服务器./config/functions/bootstrap.js或模型的生命周期钩子。
// ./api/employee/models/employee.js
module.exports = {
lifecycles: {
async afterUpdate(result, params, data) {
strapi.services.ioServer.emit('update:employee', result)
},
},
};
Run Code Online (Sandbox Code Playgroud)
小智 5
对于那些正在使用 Strapi 版本 4 寻找答案的人
var io = require("socket.io")(strapi.server.httpServer)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3352 次 |
| 最近记录: |