You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to test the webrtc-swarm library and needed a signalhub server for that purpose.
I was not sure the server.js file would work nicely alongside my existing setup, so I made a version that produces an Express router. The code is just a quick mock up, but it works for my purpose:
(Note: Code is in Typescript)
import*asexpressfrom'express'constcollect=require('stream-collector')constpump=require('pump')constiterate=require('random-iterate')constlimiter=require('size-limit-stream')consteos=require('end-of-stream')constflushHeaders=function(res){if(res.flushHeaders){res.flushHeaders()}else{if(!res._header)res._implicitHeader()res._send('')}}/** Set up a signalhub logic primarliy for webrtc-swarm, allowing the client to send and receive * on channels "all" and "user-specific" (the uuid). * * Code migrated from https://github.com/mafintosh/signalhub/blob/master/server.js */exportconstgetHubRouter=function(opts: {maxBroadcasts?: number,rootName: string}){letchannels={}letmaxBroadcasts=(opts&&opts.maxBroadcasts)||Infinityletsubs=0constrouter=express.Router()constfindName=(req: express.Request)=>req.params["app"]+"/"+req.params["channel"]/** channel = "app/channel" */constgetAppChannel=function(channel: string){if(channels[channel])returnchannels[channel]constsub={name: channel,subscribers: [],heartbeat: null}sub.heartbeat=setInterval(heartbeater(sub),30*1000)channels[channel]=subreturnchannels[channel]}router.post("/v1/:app/:channel",(req,res)=>{collect(pump(req,limiter(64*1024)),function(err,data){if(err)returnres.end()constname=findName(req)if(!channels[name])returnres.end()constchannel=getAppChannel(name)data=Buffer.concat(data).toString()letite=iterate(channel.subscribers)letnextletcnt=0while((next=ite())&&cnt++<maxBroadcasts){next.write('data: '+data+'\n\n')}res.end()})})router.get("/v1/:app/:channel",(req,res)=>{res.setHeader('Content-Type','text/event-stream; charset=utf-8')constname=findName(req)constapp=name.split('/')[0]constchannelNames=name.slice(app.length+1)channelNames.split(',').forEach(function(channelName){constchannel=getAppChannel(app+'/'+channelName)channel.subscribers.push(res)subs++eos(res,function(){subs--leti=channel.subscribers.indexOf(res)if(i>-1)channel.subscribers.splice(i,1)if(!channel.subscribers.length&&channel===channels[channel.name]){clearInterval(channel.heartbeat)deletechannels[channel.name]}})})flushHeaders(res)})returnrouter}functionheartbeater(sub){returnfunction(){for(leti=0;i<sub.subscribers.length;i++){sub.subscribers[i].write(':heartbeat signal\n\n')}}}
Then i hooked it up this way:
app.use('/signalhub/',getHubRouter({rootName: '/signalhub/'}))// No body parser!// Existing setup...app.use(bodyParser.urlencoded({extended: true}))app.use(bodyParser.json())app.use('/',eventRouter)app.use('/',router)
The text was updated successfully, but these errors were encountered:
Hi,
I wanted to test the webrtc-swarm library and needed a signalhub server for that purpose.
I was not sure the server.js file would work nicely alongside my existing setup, so I made a version that produces an Express router. The code is just a quick mock up, but it works for my purpose:
(Note: Code is in Typescript)
Then i hooked it up this way:
The text was updated successfully, but these errors were encountered: