Skip to main content

Save image from Flash using Node.js and Socket.io

I have been working on Node.js and Actionscript 3.0 for couple of day. I have searched everywhere for help to connect socket.io and flash. But i couldn't find anything decent. Most of them where outdated. Lets' check the requiremnts


  1. Node.js
  2. Socket.io 0.6
  3. FlashSocket
  4. FlexSdk
  5. Flash IDE

Before starting i would like say something about version. Flashsocket din't work on latest socket.io and i couldn't work out any solution. And also FlashSocket need FlexSdk and also websocket

Node js 

First install socket.io in to the application directory .Note the version it's 0.9


npm install socket.io@0.9
The server code will be like this

     var app = require('http').createServer(handler)
       , io = require('socket.io').listen(app,{transports:['flashsocket', 'websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']})
       , fs = require('fs');
      var port = Number(81);
     app.listen(port);
     
     function handler (req, res) {
 
     }

     io.on('connection', function (socket) {
 
  socket.on("data",function(d){
       console.log('data from flash: ',d);
       socket.emit("data","hello");
  });
   socket.on("image",function(d){
        
      var obj=JSON.parse(d);
      fs.writeFile("./images/image.png", obj.image,'base64',function (err) {
        if (err) throw err;
        console.log('It\'s saved!');
        });
  });
  socket.on('disconnect', function (socket) {
      console.log("disconnect");
  });
     });


Flash 

First you need to add websocket.swc and flexsdk to the flash classpath. Add the folder of flexsdk/frameworks/libs to the class path
Now the Actionscript Coding part
import com.pnwrain.flashsocket.FlashSocket;
import com.pnwrain.flashsocket.events.FlashSocketEvent;
import flash.display.BitmapData;
import com.adobe.images.PNGEncoder;
import flash.utils.ByteArray;
import com.hurlant.util.Base64;


var socket:FlashSocket;
var bmd:BitmapData=new Tulips();
function init():void
{
 
 socket = new FlashSocket("localhost:81");
 socket.addEventListener(FlashSocketEvent.CONNECT, onConnect);
 socket.addEventListener(FlashSocketEvent.IO_ERROR, onError);
 socket.addEventListener(FlashSocketEvent.SECURITY_ERROR, onError);
 
}

function onConnect(event:FlashSocketEvent):void
{
 
 
 
 var pngEncoder:PNGEncoder = new PNGEncoder();
 var pngStream:ByteArray = PNGEncoder.encode(bmd);
 socket.send(Base64.encodeByteArray(pngStream),"image");
 
 
 
}
function onError(event:FlashSocketEvent):void
{
 setStatus("something went wrong");
}

init();
You need as3corelib for image converting and json parsing.

What was happening here is , flash will decode the bytearray into base64 encoded string and node will save it.

I don't know it's a good idea but it seems to work for me. If there is some other way please let me know also

Comments

Unknown said…
Where do you get the websocket.swc?
Harilal K M said…
I think i find this one from here https://github.com/sinnus/socket.io-flash/tree/master/libs

I really forget about this experiment. I need to check it.
Stephen Dennis said…
Im also following this experiment. Im making a chat and need to send variables to node.js. xmlsocket.send only allows 1 field(data). i need to be able to identify that data with a tag as you have done send(encode64,"variable). this is the closest thing i have found. hope it works! :) appreciate it
Stephen Dennis said…
i got the server up and running but my swf fails during publish. i linked to websocket.swc and flex sdk. getting this messages:

C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 248 1061: Call to a possibly undefined method decode through a reference with static type Class.
C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 252 1061: Call to a possibly undefined method decode through a reference with static type Class.
C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 260 1061: Call to a possibly undefined method decode through a reference with static type Class.
C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 326 1061: Call to a possibly undefined method encode through a reference with static type Class.
C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 331 1061: Call to a possibly undefined method encode through a reference with static type Class.
C:\Users\sdennis\Desktop\nodechat\com\pnwrain\flashsocket\FlashSocket.as, Line 3 1172: Definition com.adobe.serialization.json:JSON could not be found.
Harilal K M said…
Hi stephen ,
Check the playerversion in the ide. From flash player 11 , there is inbuilt json class is there. It's better to use player<11.
Dinh Ha said…
đồng tâm
game mu
cho thuê nhà trọ
cho thuê phòng trọ
nhac san cuc manh
số điện thoại tư vấn pháp luật miễn phí
văn phòng luật
tổng đài tư vấn pháp luật
dịch vụ thành lập công ty trọn gói

Hắn đột nhiên đứng mạnh lên, vò rượu trong tay rơi xuống đất, nhìn chằm chằm vào Sở Dương.

“Ta nói là… nếu…” Sở Dương nói nhẹ nhàng.

“Nếu… Nếu…” Đại hán áo đen lẩm bẩm vô lực. Hắn đột nhiên nhớ tới vị trí mà một kiếm kia đâm tới là giữa ngực Mạc Khinh Vũ. Mà chỗ đó là nơi hội tụ của Tam âm mạch…

Sau một hồi ngẩn ngơ hắn mới trầm giọng nói: “Nếu như vậy… Cuộc đời của Tiểu thư coi như kết thúc rồi…” Trong thanh âm gã đại hán tràn đầy sự mất mát bi thương.

“Kết thúc?” Lông mày Sở Dương nhăn lại, một tia khí tức âm lãnh mơ hồ hiện ra: “Kết thúc là thế nào?”

Đại hán áo đen cay đắng cười: “Dù là gia tộc hay quốc gia thì cũng như nhau thôi. Không ít con cái của các vị gia chủ không nên thân. Bọn hắn có thể là loại quần áo lụa là, hành động ngang ngược, thậm chí một điểm võ công cũng không biết… chỉ biết làm xằng làm bậy. Tuy nhiên tất cả bọn họ đều có vị trí và giá trị của riêng mình.”

Sở Dương gật đầu trầm mặc.

“Mà tác dụng của những tên vô dụng này cũng rất lớn. Bọn hắn bình thường đều được dùng để thiết lập các mối quan hệ thông gia, củng cố thế lực của gia tộc.” Đại hán áo đen cười khổ: “Đám người cường hãn ai cũng cẩn thận cả. Hai thế lực nếu muốn liên hợp với nhau thì cần có lý do thích hợp chứ không phải ngươi muốn liên hợp với ai thì liên hợp. Một khi thế lực phát triển quá nhanh thì các thế lực khác sẽ liên hợp lại để công kích các ngươi!”

Popular posts from this blog

Configure PostgreSQL and phpPgAdmin in WAMP

As i told earlier am trying to switch to Flex , i am trying to learn connection between PHP and Flex. When searched i got a excellent tutorial . But the problem is that it uses PostgreSQL. As i don't have any prior experience in PostgreSQL i try to install it in my Windows 7 and configure it in WAMP . I got different errors and spend almost two days in searching and using different methods . Atlast i got it right. I will try to figure out the steps i have done.
Am using
PostgreSQL 9.1WAMP Server 2.0e phpPgAdmin 5.0.2 WAMP consists
1. Apache Version :2.2.17  2. PHP Version :5.3.5  3. MySQL Version :5.5.8 First install PostgreSQL , no need to install additional plugins like phpPgAdmin.
Install WAMP server where ever you need.
Download PhpPgAdmin  and unzip it to WWW folder in WAMP installation.

At this point if you try to access the http://localhost/phpPgAdmin/ you may get this error.

Your PHP installation does not support PostgreSQL. You need to recompile PHP using the --with-pgs…

Angular 4 - Modules, components and routing

In the previous post we created a new Angular 4 project. Lets check the how to add modules, components and routing.

You could check out the code from here.
git checkout -b part2.0 origin/part2.0
We are going to use bootstrap styling . So lets add bootstrap in our aplication . We are going to use only styling not the components.

npm install bootstrap@3
We need to add the bootstrap stylesheet in tha .angular-cli.json.


"styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", "styles.css" ],
Our first Module

Lets add a new module employee. Inside "src/app" run

ng generate module employee or
ng g m employee
Wait we are going to lazy load this module , so we need routing inside the module, right? So we are going to create the routing also.
ng g m employee --routing
Time for component

It will create new employee module with its own routing.lets add a component for listing the employee. So inside employee folder run

ng g c empl…

Angular - 4 year road map

AngularJS is one of the most popular JavaScript frameworks in the web. The Google developed framework is considered as one of the easiest framework to kick start a project. Two way binding and the dependency injection is the highlight of this framework.
My journey with AngularJS started about 4 years ago. Then popular ver1.4 amazed me in its capabilities. Developer like me, coming from Actionscript and plain old JQuery , AngularJS is a good option to kick start a project. It give me the confidence in JavaScript world, and from then I have tried lot of framework and tools from JavaScript itself like requireJS, webpack, etc. I did projects in Angular from 1.4 to the current 2.0, and trying to upgrade to the latest 4. Angular has now become one of the fastest growing framework. The journey starts with 1.4
One of the stable and mostly uses version. Started working on this for small projects and it seems easy for development. Dependency injection and two way binding seems quite interesting. …