https://github.com/hacknlove/wsvideo
websocket streaming html5 video
https://github.com/hacknlove/wsvideo
Last synced: 7 months ago
JSON representation
websocket streaming html5 video
- Host: GitHub
- URL: https://github.com/hacknlove/wsvideo
- Owner: hacknlove
- Created: 2017-04-06T07:54:49.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-29T07:53:39.000Z (over 8 years ago)
- Last Synced: 2025-02-13T02:07:15.855Z (over 1 year ago)
- Language: JavaScript
- Size: 68.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# wsvideo
websocket streaming html5 video
`npm install --save wsvideo`
`bower install --save wsvideo`
## Howto client-side
```
var video = document.querySelector('video')
wsvideo('ws://example.com/foo', video)
```
## Howto server-side (node example)
```
const WebSocket = require('ws')
const url = require('url')
...
var streamVideo = function (ws, path, query) {
var paused = false
var options = {}
if (query.start !== undefined) {
options.start = query.start * 1
}
if (query.end * 1) {
options.end = query.end * 1
}
///
var stream = CREATE_SOME_READ_STREAM_OF_THE_VIDEO_path_BETWEEN_options.start_AND_options.edn
///
ws.on('close', function () {
stream.destroy()
})
stream.on('data', function (chunk) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(chunk)
}
stream.pause()
// WE WANT TO SEND NOT TOO FAST
setTimeout(function () {
if (!paused) {
stream.resume()
}
}, 100)
})
ws.on('message', function (data) {
if (data === 'wait') {
paused = true
stream.pause()
return
}
if (data === 'continue') {
paused = false
return stream.resume()
}
})
stream.on('end', function () {
ws.close()
})
}
websocketserver.on('connection', function connection (ws) {
const location = url.parse(ws.upgradeReq.url, true)
return streamVideo(ws, location.pathname, location.query)
})
```
## TODO
Upload a server side