简体   繁体   English

Javascript二进制文件到服务器

[英]Javascript binary file to server

Im trying to figure out what a highly scalable solution (serving multiple users of 10000+) would be. 我试图弄清楚什么是高度可扩展的解决方案(服务于10000+的多个用户)。

Goal: What I want to achieve is to write a stream of mouse coordinates to a binary file to a server, ie the server is directly saved to the server from the userinteraction once stream is closed. 目标:我想要实现的是将鼠标坐标流写入二进制文件到服务器,即,一旦流关闭,服务器就可以从用户交互中直接保存到服务器。 The coordinates should be pushed every 20ms (about 50fps) to create a close representation of the mouse movement. 应每20毫秒(约50 fps)推动一次坐标,以创建鼠标移动的近似表示。

a) I know that nodeJS can be used to do a writestream, but I am not sure if such a high frequency of updates can be handled by such a structure - also if this is done by multiple users, this approach could fall apart. a)我知道可以使用nodeJS进行写流,但是我不确定这种结构是否可以处理如此频繁的更新-同样,如果这是由多个用户完成的,则这种方法可能会瓦解。

b)The other possibility would be to locally write the file into binary and then upload it to the server afterwards. b)另一种可能性是将文件本地写入二进制文件,然后再将其上传到服务器。

Can anyone comment on the capabilities of these approaches and if there is another method that can be used? 任何人都可以评论这些方法的功能以及是否可以使用另一种方法?

Based on your comment that the server does not need the data in real-time, you should definitely not send the data to the server every 20ms. 基于您的评论,即服务器不需要实时数据,您绝对不应每20毫秒将数据发送到服务器。 Save it on the client and send it in chunks, say every 30 or 60 seconds. 将其保存在客户端上并分块发送,例如每30或60秒发送一次。 This can be done in memory in an array. 这可以在数组的内存中完成。 Saving mouse coordinates is not very intensive. 保存鼠标坐标不是很费力。

Once you're ready to send it, I think you'll find XMLHttpRequest sufficiently fast enough for your needs. 一旦准备好发送它,我认为您会发现XMLHttpRequest足够快地满足您的需求。

This demo shows how to capture to coordinates and shows that 50fps is doable (I get up to 60fps in Chrome on OS X). 该演示演示了如何捕获坐标并显示了50fps可行(我在OS X上的Chrome中达到60fps)。

Demo: http://jsfiddle.net/ThinkingStiff/2Ls3A/ 演示: http//jsfiddle.net/ThinkingStiff/2Ls3A/

var coordinates = [],
    count = 0,
    SECONDS = 1000,
    INTERVAL = 5; 

window.onmousemove = function ( event ) {

    //move cursor over page for at least 10 secs for an accurate reading
    coordinates.push( [event.pageX, event.pageY] );

};

window.setInterval( function () {

    console.log( ( coordinates.length - count ) / INTERVAL + 'fps' );
    count = coordinates.length;

}, INTERVAL * SECONDS ); 

If you really think you need more efficiency in the sending of the data, the lowest-latency method to connect a client and server computer over the internet is a persistent TCP socket. 如果您确实认为需要更高的数据发送效率,则通过Internet连接客户端和服务器计算机的最低延迟方法是持久的TCP套接字。 This is available in HTML through the WebSocket API . 可通过WebSocket API在HTML中使用。

There are libraries in just about every server-side language that handle the end-point using the ws: URL schema ( wss: for secure), socket.io for example. 几乎每种服务器端语言都有使用ws: URL架构(例如wss:表示安全),例如socket.io处理端点的库。

Client: 客户:

var socket = new WebSocket('ws://example.com/endpoint' );

socket.onopen = function () {

    socket.send( 'send some text' );

};

I would think a better solution is to store the points into an array, and then turn around and send it to the server. 我认为更好的解决方案是将点存储到数组中,然后转过来并将其发送到服务器。

At this point you need to start a new object and have it get the new updates, when you go into the function to send to the server, this way, during the time that you are preparing and sending the data, and getting a response, you can still be collecting data. 此时,当您进入准备发送和发送数据并获得响应的功能时,您需要启动一个新对象并使其获得新的更新,以这种方式发送到服务器。您仍然可以收集数据。

But, you probably won't get 50fps, but you can test on different browsers and see what frame rate is reasonable to expect. 但是,您可能不会获得50fps,但是您可以在不同的浏览器上进行测试,并查看合理预期的帧速率。

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

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