-
-
Save clevertonh/22261a9a1660296fcb1b90d7b315276f to your computer and use it in GitHub Desktop.
Image over 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
| //NodeJS | |
| var express = require('express'); | |
| var app = express(); | |
| var http = require('http').Server(app); | |
| var fs = require('fs'); | |
| var io = require('socket.io')(http); | |
| app.use(express.static(__dirname, '/')); | |
| io.on('connection', function(socket){ | |
| fs.readFile('image.png', function(err, data){ | |
| socket.emit('imageConversionByClient', { image: true, buffer: data }); | |
| socket.emit('imageConversionByServer', "data:image/png;base64,"+ data.toString("base64")); | |
| }); | |
| }); | |
| http.listen(3000, function(){ | |
| console.log('listening on *:3000'); | |
| }); | |
| ///////////////// | |
| <!-- Browser JavaScript --> | |
| <img id="img" width="40%"> | |
| <img id="img2" width="40%"> | |
| <script src="/jquery-2.1.1.js"></script> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <script src="/socket.io-stream.js"></script> | |
| <script> | |
| function b64(e){var t="";var n=new Uint8Array(e);var r=n.byteLength;for(var i=0;i<r;i++){t+=String.fromCharCode(n[i])}return window.btoa(t)} | |
| $(document).ready(function() { | |
| var socket = io(); | |
| socket.on('imageConversionByClient', function(data) { | |
| $("#img").attr("src","data:image/png;base64,"+b64(data.buffer)); | |
| }); | |
| socket.on('imageConversionByServer', function(data) { | |
| $("#img2").attr("src",data); | |
| }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment