Created
March 5, 2015 21:48
-
-
Save mmaelzer/39bc2cacaf5f3ee50a23 to your computer and use it in GitHub Desktop.
mjpeg-camera + socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MjpegCamera = require('mjpeg-camera'); | |
var WriteStream = require('stream').Writable; | |
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var camera = new MjpegCamera({ | |
user: 'username', | |
password: 'password', | |
url: 'http://camera-ip-address', | |
name: 'camera' | |
}); | |
camera.start(); | |
var ws; | |
io.on('connection', function(socket) { | |
ws = new WriteStream({objectMode: true}); | |
s._write = function(chunk, enc, next) { | |
var jpeg = chunk.data.toString('base64'); | |
socket.emit('frame', {data: jpeg}); | |
next(); | |
}; | |
camera.pipe(ws); | |
}); | |
io.on('disconnect', function() { | |
camera.unpipe(ws); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment