发送和接收的Javascript网页上的插座的二进制数据? [英] Send and receive binary data over web sockets in Javascript?

查看:217
本文介绍了发送和接收的Javascript网页上的插座的二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能发送和接收的Javascript网页上的插座的二进制数据?我能,例如,使用网络套接字实现SSH客户端?


解决方案

接下来的草案( hybi -07 )规范在大多数浏览器正在实施的WebSockets的,它会增加内置的协议和API的二进制支持。

不过,在此之前,有效载荷的WebSockets为en codeD为UTF-8。为了发送二进制数据,你必须使用二进制数据为UTF-8编码的一些方法。

有很多选择,但这里有两个,我用:

UTF-8

实际上,你可以连接code字节流直接为UTF-8。

蟒蛇为en code和德code会是这个样子:

 从codeCS进口(utf_8_en code,utf_8_de code,
                    latin_1_en code,latin_1_de code)utf_8_en code(UNI code(BUF,拉丁-1'))[0]#连接codelatin_1_en code(utf_8_de code(utf8_buf)[0])[0]#德code

在使用Javascript:

  CHR = data.char $ C $猫(N)//以去code'的消息的位置N//字节数组Enocde(0-255)为UTF-8
数据= array.map(功能(NUM){
    返回String.fromChar code(NUM); })。加入('');

UTF-8 EN code注:


  • 有关被均匀地值0-255分布的二进制数据,则有效载荷的大小比原始二进制数据大50%。


  • 闪存的WebSockets仿真器网络插座JS 可能有麻烦的0编码(零)。


基本64

在Python的:

 从进口的base64 b64en code,b64de code数据= b64en code(BUF)#EN code二进制缓冲区B64BUF = b64de code(数据)#德code B64以二进制缓冲区

要带code和德code的Javascript世界的信息:

 数据= window.btoa(MSG)// EN code为base64味精= window.atob(数据)//德code的base64
msg.char $ C $猫(N)//读取德code为N字节

64相应的注意事项:


  • 均匀分布的二进制数据(0-255)会比原始数据大33%。


  • 有少蟒蛇方开销base64编码之外还有为UTF-8编码。然而,有更多的JavaScript端的开销解码的base64(UTF-8不需要在Javascript解码,因为浏览器已经转换的UTF-8给Javascript本地UTF-16)。


  • 更新:这是假设二进制数据连接coded到一个UTF-8字符串作为与从0到255的范围字符值如上图所示。具体来说,window.atob不支持超过255字符值见本 Mozilla浏览器的bug 。同样的限制适用于Chrome浏览器。


websockify

WebSockify 是一个代理器/桥接器,其允许的WebSockets的浏览器与任何任意二进制服务进行通信。它的建立,让 noVNC 的与现有的VNC服务器进行通信。 websockify使用的base64恩二进制数据的code /德code,并且还为在Javascript中使用 websock.js 库。在 websock.js 也有类似的常规的WebSocket的API,但它是透明地处理二进制数据,并设计了websockify沟通。 免责声明:我创建websockify和noVNC。

SSH客户端

从技术上讲,你可以实现的WebSockets以上浏览器SSH客户端(并且我认为它),但是,这需要做SSH加密和解密的,这将是缓慢的浏览器。由于拥有的WebSockets加密的WSS(TLS)模式下,它可能更有意义上的WebSocket WSS做普通的telnet。

事实上,websockify包括例如Telnet客户端。

您将推出websockify在这样的主机名(telnetd的是KRB5-的telnetd):

 须藤./websockify 2023 --web。 --wrap模式=重生 -  telnetd的-debug 2023

然后导航到的http://主机名:2023 / wstelnet.html主机= HOSTNAME&安培;港口= 2023

查看 websockify自述了解详情。要使用WSS加密,你需要创建一个SSL密钥对 noVNC高级用法维基页面 <描述/ p>

It is possible to send and receive binary data over web sockets in Javascript? Could I, for example, implement an SSH client using web sockets?

解决方案

The next draft (hybi-07) of the WebSockets specification is being implemented in most browsers and it will add built-in binary support to the protocol and API.

However, until then, WebSockets payload is encoded as UTF-8. In order to send binary data you must use some way of encoding the binary data as UTF-8.

There are many options but here are two that I have used:

UTF-8:

You can actually encode a byte stream directly to UTF-8.

The python to encode and decode would look something like this:

from codecs import (utf_8_encode, utf_8_decode,
                    latin_1_encode, latin_1_decode)

utf_8_encode(unicode(buf, 'latin-1'))[0]      # encode

latin_1_encode(utf_8_decode(utf8_buf)[0])[0]  # decode

In Javascript:

chr = data.charCodeAt(N)  // to 'decode' at position N of the message

// Enocde array of bytes (0-255) to UTF-8
data = array.map(function (num) {
    return String.fromCharCode(num); }).join('');

UTF-8 encode notes:

  • For binary data that is evenly distributed across value 0-255, then size of the payload is 50% larger than the raw binary data.

  • The Flash WebSockets emulator web-socket-js may have trouble with the encoding of 0 (zero).

Base 64:

In python:

from base64 import b64encode, b64decode

data = b64encode(buf)    # encode binary buffer to b64

buf = b64decode(data)    # decode b64 to binary buffer

To encode and decode the messages on the Javascript side:

data = window.btoa(msg)  // Encode to base64

msg = window.atob(data)  // Decode base64
msg.charCodeAt(N)        // Read decode byte at N

Base 64 notes:

  • Evenly distributed binary data (0-255) will be 33% larger than the raw data.

  • There is less python side overhead to base64 encoding than there is to UTF-8 encoding. However, there is a bit more Javascript side overhead to decoding base64 (UTF-8 doesn't need decoding in Javascript since the browser has already converted the UTF-8 to the Javascript native UTF-16).

  • Update: This assumes the binary data is encoded to a UTF-8 string as shown above with character values that range from 0-255. Specifically, window.atob does not support character values above 255. See this mozilla bug. The same limitation applies to Chrome.

websockify:

WebSockify is a proxy/bridge that allows a WebSockets capable browser to communicate with any arbitrary binary service. It was created to allow noVNC to communicate with existing VNC servers. websockify uses base64 encode/decode of the binary data and also provides a websock.js library for use in Javascript. The websock.js has an API similar to regular WebSocket but it is handles binary data transparently and is designed to communicate with websockify. Disclaimer: I created websockify and noVNC.

ssh client:

Technically you could implement a browser ssh client over WebSockets (and I've considered it), however, this will require doing SSH encryption and decryption in the browser which will be slow. Given that WebSockets has an encrypted WSS (TLS) mode, it probably makes more sense to do plain telnet over WebSocket WSS.

In fact, websockify includes an example telnet client.

You would launch websockify on HOSTNAME like this (telnetd is from krb5-telnetd):

sudo ./websockify 2023 --web . --wrap-mode=respawn -- telnetd -debug 2023

Then navigate to http://HOSTNAME:2023/wstelnet.html?hostname=HOSTNAME&port=2023

See the websockify README for more information. To use WSS encryption you will need to create an SSL key as described on the noVNC advanced usage wiki page

这篇关于发送和接收的Javascript网页上的插座的二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆