@11ways/socket.io-stream 中文文档教程
Socket.IO stream
这是通过 Socket.IO 使用 Stream API 进行双向二进制数据传输的模块。
这是 nkzawa/socket.io-stream 的一个分支,主要是为了升级老化的依赖项。 在制品。
Installation
npm install @11ways/socket.io-stream
Usage
如果您不熟悉 Stream API,请务必查看文档。 我还建议您查看很棒的 Stream Handbook。
对于服务器和客户端之间的流式传输,您将首先发送流实例。 要接收流,您只需将 socket
包装在 socket.io-stream
中,然后照常收听任何事件。
服务器:
var io = require('socket.io').listen(80);
var ss = require('@11ways/socket.io-stream');
var path = require('path');
io.of('/user').on('connection', function(socket) {
ss(socket).on('profile-image', function(stream, data) {
var filename = path.basename(data.name);
stream.pipe(fs.createWriteStream(filename));
});
});
createStream()
返回可由 emit()
发送的新流。
客户端:
var io = require('socket.io-client');
var ss = require('@11ways/socket.io-stream');
var socket = io.connect('http://example.com/user');
var stream = ss.createStream();
var filename = 'profile.jpg';
ss(socket).emit('profile-image', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);
您可以将数据从客户端流式传输到服务器,反之亦然。
// send data
ss(socket).on('file', function(stream) {
fs.createReadStream('/path/to/file').pipe(stream);
});
// receive data
ss(socket).emit('file', stream);
stream.pipe(fs.createWriteStream('file.txt'));
Browser
该模块可以在浏览器上使用。 为此,只需将文件复制到公共目录即可。
$ cp node_modules/socket.io-stream/socket.io-stream.js somewhere/public/
您还可以使用 browserify 创建您自己的包。
$ npm install browserify -g
$ cd node_modules/socket.io-stream
$ browserify index.js -s ss > socket.io-stream.js
<input id="file" type="file" />
<script src="/socket.io/socket.io.js"></script>
<script src="/js/socket.io-stream.js"></script>
<script src="/js/jquery.js"></script>
<script>
$(function() {
var socket = io.connect('/foo');
$('#file').change(function(e) {
var file = e.target.files[0];
var stream = ss.createStream();
// upload a file to the server.
ss(socket).emit('file', stream, {size: file.size});
ss.createBlobReadStream(file).pipe(stream);
});
});
</script>
Upload progress
您可以像下面这样跟踪上传进度:
var blobStream = ss.createBlobReadStream(file);
var size = 0;
blobStream.on('data', function(chunk) {
size += chunk.length;
console.log(Math.floor(size / file.size * 100) + '%');
// -> e.g. '42%'
});
blobStream.pipe(stream);
Socket.IO v0.9 support
在将库与 socket.io v0.9.x 一起使用时,您必须设置 forceBase64
选项 true
。
ss.forceBase64 = true;
Documentation
ss(sio)
- sio
socket.io Socket
A socket of Socket.IO, both for client and server - return
Socket
查找基于sio
(Socket.IO 的套接字)的现有Socket
实例,如果不存在则创建一个。
socket.emit(event, [arg1], [arg2], […])
- event
String
The event name
发出一个带有可变数量参数的 event
,至少包括一个流。
ss(socket).emit('myevent', stream, {name: 'thefilename'}, function() { ... });
// send some streams at a time.
ss(socket).emit('multiple-streams', stream1, stream2);
// as members of array or object.
ss(socket).emit('flexible', [stream1, { foo: stream2 }]);
// get streams through the ack callback
ss(socket).emit('ack', function(stream1, stream2) { ... });
socket.on(event, listener)
- event
String
The event name - listener
Function
The event handler function
为 event
添加一个 listener
。 listener
将带有任何数据的流作为参数。
ss(socket).on('myevent', function(stream, data, callback) { ... });
// access stream options
ss(socket).on('foo', function(stream) {
if (stream.options && stream.options.highWaterMark > 1024) {
console.error('Too big highWaterMark.');
return;
}
});
ss.createStream([options])
- options
Object
- highWaterMark
Number
- encoding
String
- decodeStrings
Boolean
- objectMode
Boolean
- allowHalfOpen
Boolean
iftrue
, then the stream won't automatically close when the other endpoint ends. Default tofalse
.
- highWaterMark
- return
Duplex Stream
创建一个新的双工流。 有关流和选项
的详细信息,请参阅文档。
var stream = ss.createStream();
// with options
var stream = ss.createStream({
highWaterMark: 1024,
objectMode: true,
allowHalfOpen: true
});
ss.createBlobReadStream(blob, [options])
- options
Object
- highWaterMark
Number
- encoding
String
- objectMode
Boolean
- highWaterMark
- return
Readable Stream
为 Blob 和 File 在浏览器上。 有关流和选项
的详细信息,请参阅文档。
var stream = ss.createBlobReadStream(new Blob([1, 2, 3]));
ss.Buffer
在浏览器上使用的 Node Buffer 类,为了方便而公开。 在 Node 环境中,您应该只使用普通的 Buffer
。
var stream = ss.createStream();
stream.write(new ss.Buffer([0, 1, 2]));
License
麻省理工学院
Socket.IO stream
This is the module for bidirectional binary data transfer with Stream API through Socket.IO.
This is a fork of nkzawa/socket.io-stream, mainly just to upgrade the aging dependencies. WIP.
Installation
npm install @11ways/socket.io-stream
Usage
If you are not familiar with Stream API, be sure to check out the docs. I also recommend checking out the awesome Stream Handbook.
For streaming between server and client, you will send stream instances first. To receive streams, you just wrap socket
with socket.io-stream
, then listen any events as usual.
Server:
var io = require('socket.io').listen(80);
var ss = require('@11ways/socket.io-stream');
var path = require('path');
io.of('/user').on('connection', function(socket) {
ss(socket).on('profile-image', function(stream, data) {
var filename = path.basename(data.name);
stream.pipe(fs.createWriteStream(filename));
});
});
createStream()
returns a new stream which can be sent by emit()
.
Client:
var io = require('socket.io-client');
var ss = require('@11ways/socket.io-stream');
var socket = io.connect('http://example.com/user');
var stream = ss.createStream();
var filename = 'profile.jpg';
ss(socket).emit('profile-image', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);
You can stream data from a client to server, and vice versa.
// send data
ss(socket).on('file', function(stream) {
fs.createReadStream('/path/to/file').pipe(stream);
});
// receive data
ss(socket).emit('file', stream);
stream.pipe(fs.createWriteStream('file.txt'));
Browser
This module can be used on the browser. To do so, just copy a file to a public directory.
$ cp node_modules/socket.io-stream/socket.io-stream.js somewhere/public/
You can also use browserify to create your own bundle.
$ npm install browserify -g
$ cd node_modules/socket.io-stream
$ browserify index.js -s ss > socket.io-stream.js
<input id="file" type="file" />
<script src="/socket.io/socket.io.js"></script>
<script src="/js/socket.io-stream.js"></script>
<script src="/js/jquery.js"></script>
<script>
$(function() {
var socket = io.connect('/foo');
$('#file').change(function(e) {
var file = e.target.files[0];
var stream = ss.createStream();
// upload a file to the server.
ss(socket).emit('file', stream, {size: file.size});
ss.createBlobReadStream(file).pipe(stream);
});
});
</script>
Upload progress
You can track upload progress like the following:
var blobStream = ss.createBlobReadStream(file);
var size = 0;
blobStream.on('data', function(chunk) {
size += chunk.length;
console.log(Math.floor(size / file.size * 100) + '%');
// -> e.g. '42%'
});
blobStream.pipe(stream);
Socket.IO v0.9 support
You have to set forceBase64
option true
when using the library with socket.io v0.9.x.
ss.forceBase64 = true;
Documentation
ss(sio)
- sio
socket.io Socket
A socket of Socket.IO, both for client and server - return
Socket
Look up an existing Socket
instance based on sio
(a socket of Socket.IO), or create one if it doesn't exist.
socket.emit(event, [arg1], [arg2], […])
- event
String
The event name
Emit an event
with variable number of arguments including at least a stream.
ss(socket).emit('myevent', stream, {name: 'thefilename'}, function() { ... });
// send some streams at a time.
ss(socket).emit('multiple-streams', stream1, stream2);
// as members of array or object.
ss(socket).emit('flexible', [stream1, { foo: stream2 }]);
// get streams through the ack callback
ss(socket).emit('ack', function(stream1, stream2) { ... });
socket.on(event, listener)
- event
String
The event name - listener
Function
The event handler function
Add a listener
for event
. listener
will take stream(s) with any data as arguments.
ss(socket).on('myevent', function(stream, data, callback) { ... });
// access stream options
ss(socket).on('foo', function(stream) {
if (stream.options && stream.options.highWaterMark > 1024) {
console.error('Too big highWaterMark.');
return;
}
});
ss.createStream([options])
- options
Object
- highWaterMark
Number
- encoding
String
- decodeStrings
Boolean
- objectMode
Boolean
- allowHalfOpen
Boolean
iftrue
, then the stream won't automatically close when the other endpoint ends. Default tofalse
.
- highWaterMark
- return
Duplex Stream
Create a new duplex stream. See the docs for the details of stream and options
.
var stream = ss.createStream();
// with options
var stream = ss.createStream({
highWaterMark: 1024,
objectMode: true,
allowHalfOpen: true
});
ss.createBlobReadStream(blob, [options])
- options
Object
- highWaterMark
Number
- encoding
String
- objectMode
Boolean
- highWaterMark
- return
Readable Stream
Create a new readable stream for Blob and File on browser. See the docs for the details of stream and options
.
var stream = ss.createBlobReadStream(new Blob([1, 2, 3]));
ss.Buffer
Node Buffer class to use on browser, which is exposed for convenience. On Node environment, you should just use normal Buffer
.
var stream = ss.createStream();
stream.write(new ss.Buffer([0, 1, 2]));
License
MIT