使用WebSocket进行文件传输

发布于 2025-01-03 23:59:30 字数 178 浏览 1 评论 0原文

我的一位大学讲师指出,看到 WebSockets 用于文件传输会很有趣。我想象可以使用 base64 解码和编码图像文件,但是 是否可以通过 WebSocket 发送 JavaScript / CSS 文件?

我使用的服务器是 Node.js,我的浏览器是 Google Chrome 16。

One of my university lecturers pointed out that it would be interesting to see WebSockets used for file transfer. I'd imagine it would be possible to decode and encode an image file using base64, however would it be possible to send JavaScript / CSS files via WebSocket?

The server i'm using is Node.js, and my browser is Google Chrome 16.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2025-01-10 23:59:30

。您可以通过 WebSockets(或 AJAX)发送 JavaScript 和 CSS。只要 WebSocket 服务器正确地对 Javascript 中的任何特殊 Unicode 字符进行 UTF-8 编码,您也不需要像对图像那样对 CSS 和 JavaScript 进行 Base64 编码。

通过 WebSocket 收到 Javascript 或 CSS 后,您可以使用以下机制加载它们(其中类型为“script”或“css”):

function dynamic_load(type, content) {
    var elem = document.createElement(type);
    elem.type = (type === 'script') ? 'text/javascript' : 'text/css';
    elem.innerHTML = content;
    document.getElementsByTagName("head")[0].appendChild(elem);
}

该机制在 IE 8 及更早版本中可能会遇到问题,但由于您使用的是 WebSockets 我怀疑您的目标是现代浏览器。您可以从浏览器的 Javascript 控制台验证dynamic_load函数是否正常工作:

dynamic_load('script', "alert('hello world');");

Yes. You can send JavaScript and CSS via WebSockets (or AJAX for that matter). You also shouldn't need to base64 encode the CSS and JavaScript like you would an image as long as the WebSocket server is properly UTF-8 encoding any special Unicode characters in the Javascript.

Once you have received the Javascript or CSS via WebSocket, you can load them using the following mechanism (where type is either 'script' or 'css'):

function dynamic_load(type, content) {
    var elem = document.createElement(type);
    elem.type = (type === 'script') ? 'text/javascript' : 'text/css';
    elem.innerHTML = content;
    document.getElementsByTagName("head")[0].appendChild(elem);
}

That mechanism may have trouble in IE 8 and earlier but since you are using WebSockets I suspect your target is modern browsers. You can verify that the dynamic_load function works from your browser's Javascript console:

dynamic_load('script', "alert('hello world');");
蓝天白云 2025-01-10 23:59:30

我的node.js ws 库处理文件发送——甚至是二进制文件。查看此处的示例之一,该示例进行上传: https://github.com /einaros/ws/tree/master/examples/fileapi

不过,我建议坚持使用 SPDY,而不是使用 websockets 来接收网页资源(脚本、css、图像等)——这是有意设计的为此很有目的。顺便说一下,Node.js 有 spdy 支持(请参阅 https://github.com/indutny/node- spdy)。

My node.js ws library handles file sends -- even binary ones. Check out one of the examples here, which does uploads: https://github.com/einaros/ws/tree/master/examples/fileapi

Rather than using websockets for receiving the webpage assets (scripts, css, images, etc), however, I'd recommend sticking with SPDY -- which was intentionally crafted for that very purpose. Node.js has spdy-support, by the way (see https://github.com/indutny/node-spdy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文