简体   繁体   English

Socket.io 与 Adonis.js

[英]Socket.io with Adonis.js

I am using Adonis 4.1.0 and Adonis-websocket is only been available for v3 .我正在使用 Adonis 4.1.0 并且Adonis-websocket仅适用于v3 Can anyone tell me workaround for using socket.io with Adonis 4.1.0?谁能告诉我将socket.io与 Adonis 4.1.0 一起使用的解决方法?

apparently they have been working on this not long ago, it was based on socket.io but because of some issues like memory leaks and others, they decided to use websockets directly instead, check these discussions:显然他们不久前一直在研究这个,它是基于socket.io但由于内存泄漏等问题,他们决定直接使用websockets ,检查这些讨论:
https://github.com/adonisjs/discussion/issues/51 https://github.com/adonisjs/discussion/issues/51
https://forum.adonisjs.com/t/integrating-socket-io-with-adonis-4/519 https://forum.adonisjs.com/t/integrating-socket-io-with-adonis-4/519

have you tried using socket.io without relying on Adonis ?您是否尝试过在不依赖Adonis的情况下使用socket.io , something like: , 就像是:

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');
});

But you should be able to do this with Adonis by now according to: https://github.com/adonisjs/adonis-websocket-protocol但是你现在应该能够根据以下内容使用Adonis执行此操作: https ://github.com/adonisjs/adonis-websocket-protocol

Example:例子:

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)
  }
}

check this for broadcast examples using WS : https://github.com/websockets/ws#broadcast-example使用WS检查广播示例: https ://github.com/websockets/ws#broadcast-example

Create start/socket.js file and paste following code inside it.创建 start/socket.js 文件并在其中粘贴以下代码。

const Server = use('Server')
const io = use('socket.io')(Server.getInstance())

io.on('connection', function (socket) {
console.log(socket.id)
})

From Virk Himself in this forum: https://forum.adonisjs.com/t/integrating-socket-io-with-adonis-4/519来自本论坛的 Virk 本人: https ://forum.adonisjs.com/t/integrating-socket-io-with-adonis-4/519

create a standalone socket io configuration file in start/socket.js在 start/socket.js 中创建一个独立的套接字 io 配置文件

const io = require('socket.io')();
io.listen(3000);
io.on('connection', function (socket) {
  console.log(socket.id)
})

to start your socket io server you can configure your server.js as below要启动你的 socket io 服务器,你可以如下配置你的 server.js

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .preLoad('start/socket') //path of socket.js
  .fireHttpServer()
  .catch(console.error)

now when you start your server then it will start along with socket io现在,当您启动服务器时,它将与套接字 io 一起启动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM