最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to define socket variable globally - Stack Overflow

programmeradmin4浏览0评论

I have this piece of code in my socketio file and here I can use socket simply.

import _ from 'lodash'
import mongoose from 'mongoose'

exports.register = (server, options, next) => {
  var io = require('socket.io')(server.listener)
  io.on('connection', async(socket) => {
    // here I can use socket.emit() and all
  })
  next()
}

exports.register.attributes = {
  name: 'socket'
}

Now, I need to use theio socket to emit events from various files and don't want to connect this io.on('connection', async(socket) => {}) every time.

How can I do this?

Thank you!!!

I have this piece of code in my socketio file and here I can use socket simply.

import _ from 'lodash'
import mongoose from 'mongoose'

exports.register = (server, options, next) => {
  var io = require('socket.io')(server.listener)
  io.on('connection', async(socket) => {
    // here I can use socket.emit() and all
  })
  next()
}

exports.register.attributes = {
  name: 'socket'
}

Now, I need to use theio socket to emit events from various files and don't want to connect this io.on('connection', async(socket) => {}) every time.

How can I do this?

Thank you!!!

Share Improve this question edited Apr 12, 2019 at 7:45 Profer asked Apr 4, 2019 at 7:19 ProferProfer 64310 gold badges47 silver badges97 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

next callback doesn't serve a good purpose here because it's synchronous. Since socket.io connection event can be triggered multiple times, it cannot be converted to a promise for easier chaining, so it's better for it to stay callback-based.

It can be:

var socketIo = require('socket.io')

exports.register = (server, options) => {
  var io = socketIo(server.listener);

  return onConnect => {
    io.on('connection', onConnect);
  };
}

So connection function is created once:

const myConnection = register(myServer);

And used through the application:

myConnection(client => {
  ...
});

This situation can also benefit from observables, e.g. RxJS.

In case the socket shouldn't support reconnections, this could be simplified to:

exports.register = (server, options) => {
  var io = socketIo(server.listener);

  return new Promise(resolve => {        
    io.once('connection', resolve);
  });
}

Connection promise is created once:

So connection function is created once:

const myConnection = register(myServer);

And used through the application:

const client = await myConnection;
...

You can share functionality across your server instance with Server methods

import _ from 'lodash'
import mongoose from 'mongoose'

exports.register = (server, options, next) => {
  var io = require('socket.io')(server.listener)
  io.on('connection', async(socket) => {
    // here I can use socket.emit() and all
  })
  // here use server methods
  server.decorate('server', 'io', io);
  next()
}

exports.register.attributes = {
  name: 'socket'
}

Then in your controller

handler: function (request, reply) {
        const io = request.server.io;

    }
发布评论

评论列表(0)

  1. 暂无评论