gpt4 book ai didi

node.js - Socket.io 与 Adonis.js

转载 作者:搜寻专家 更新时间:2023-10-31 23:19:43 25 4
gpt4 key购买 nike

我使用的是 Adonis 4.1.0,Adonis-websocket 仅适用于 v3。谁能告诉我在 Adonis 4.1.0 中使用 socket.io 的解决方法?

最佳答案

显然他们不久前就在研究这个,它基于 socket.io 但由于内存泄漏等问题,他们决定使用 websockets 相反,直接查看这些讨论:
https://github.com/adonisjs/discussion/issues/51
https://forum.adonisjs.com/t/integrating-socket-io-with-adonis-4/519

您是否尝试过使用 socket.io 而不依赖于 Adonis ? ,类似的东西:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

但是根据 https://github.com/adonisjs/adonis-websocket-protocol,您现在应该可以用 Adonis 来做到这一点了。

示例:

const filereader = require('simple-filereader')
const msgpack = require('msgpack-lite')
const packets = require('@adonisjs/websocket-packets')

const client = new WebSocket('ws://localhost:3000/adonis-ws')

client.onopen = function () {
// TCP connection created
}

client.onerror = function () {
// TCP connection error
}

client.onmessage = function (message) {
filereader(message, function (error, payload) {
const packet = msgpack.decode(payload)
handlePacket(packet)
})
}

function handlePacket (packet) {
if (packets.isOpenPacket(packet)) {
console.log('Server ack connection. Make channel subscriptions now')
}

if (packets.isJoinAck(packet)) {
console.log('subscription created for %s', packet.d.topic)
}
}

使用 WS 查看广播示例:https://github.com/websockets/ws#broadcast-example

关于node.js - Socket.io 与 Adonis.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49356813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com