简体   繁体   English

每秒发出的socket.io

[英]socket.io emitting every second

Really quick question. 真的很简单。

I'm creating a synced streaming app so I would like to emit the current timecode of what's being played every second through socket.io and broadcast it down to other clients. 我正在创建一个同步的流应用程序,因此我想通过socket.io发出每秒正在播放的内容的当前时间码,并将其广播给其他客户端。

Is this wise? 这明智吗? Are there any drawbacks to making the same call every second, can I make other calls at the same time? 每秒拨打同一电话有任何弊端,我可以同时拨打其他电话吗?

PS I'm not making any database calls or doing any processing server-side. PS我没有进行任何数据库调用或在服务器端进行任何处理。

with not to many clients viewing videos it should be fine, but eventually you will experience small lags if the number of users viewing starts to get big. 观看视频的客户不多,应该没问题,但是最终,如果观看的用户数量开始增加,您将遇到小的滞后。

Another approach is to keep track on server for example you can do this 另一种方法是跟踪服务器,例如,您可以执行此操作

Video loads with autoplay or emit an event to start a timer server-side 视频会自动播放加载或发出事件以启动计时器服务器端

// from client-side
socket.emit('videoplaying',/* some video data */);


on server side you start small timers based on socket IDs 在服务器端,您可以根据套接字ID启动小型计时器

 function Timer(VideoInformation){ this.currentTime=0; this.startedAt=+new Date(); this.endedAt=null; this.title=VideoInformation.title||'Untitled'; this.interval=null; this.play=function(){ var self=this; this.interval=setInterval(function(){ self.currentTime=+new Date(); },1000); } this.stop=function(){ if(this.interval!==null){ clearInterval(this.interval) } } //.. pause/end/reset .. } //server side var TimeTracker={}; // handling new videoplaying socket.on('videoplaying',function(videoInformation){ if(!TimeTracker.hasOwnProperty(socket.id)){ TimeTracker[socket.id]=[]; } TimeTracker[socket.id].push(new Timer(videoInformation)); }); 


In the end you add event listeners to current video the user is viewing to notify the server timer that it has paused/stopped/click on specific video time etc.. 最后,您将事件侦听器添加到用户正在查看的当前视频中,以通知服务器计时器它已暂停/停止/单击特定的视频时间等。

Hope it helps, this isn't a working solution, its more a concept.. 希望它会有所帮助,这不是一个可行的解决方案,而是一个概念。

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

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