Just started to learn javascript and angular. I use the Poco library to create a server. I send the picture as follows:
int s, len; // здесь загружаю картинку из файла в буфер. char* buff = CWebSocketServer::FileToBuffer( len ); WebSocket ws( request, response ); s = ws.sendFrame( buff, len, WebSocket::FRAME_BINARY ); // 54682 передано The client on AngularJS accepts data:
var ws = new WebSocket("ws://127.0.0.1:8000/ws"); ... ws.onmessage = function (msg) { console.log("Message received: " + msg.data ); // [object Blob] принят console.log("Message length: " + msg.data.size); // 54682 байта в блобе $scope.image = window.btoa(msg.data); $scope.$apply(); } HTML:
<img ng-src="data:image/JPEG;base64,{{image}}"> What else is missing to display the picture? Am I doing right at all?
ws.onmessageeventws.onmessageis outside of the angular context, it does not know that the$scope.imagevariable has$scope.imageupdated. Try it after$scope.image = window.btoa(msg.data);write$scope.$apply();. - Stepan Kasyanenkosrcattribute of theimgelement. You can see in the browser console. - Stepan Kasyanenkodocument.getElementById('myImg').src="data:image/JPEG;base64,"+window.btoa(msg.data). - Stepan Kasyanenko