2014年8月19日火曜日

ActionScript3.0とNode.jsでSocket通信をする

今回は、Node.jsとSocket通信でメッセージを送りたい場合のお話。

前回まではレイヤーに書いてましたが、今回はクラスにActionScriptを読み込みます。



















ソースは以下のとおり。

SocketEx.as

package
{
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.XMLSocket;

public class SocketEx extends Sprite
{
private var _xmlSocket:XMLSocket; //ソケット
 
 
// --------------------------------------
// コンストラクタ
// --------------------------------------
public function SocketEx()
{
// EVENTS
addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
 
}
 
// --------------------------------------
// メソッド
// --------------------------------------
//ステージの追加とソケットの接続
private function _onAddedToStage(aEvent : Event):void
{

_xmlSocket = new XMLSocket("153.127.250.224", 3000);
 
// EVENTS
stage.removeEventListener(MouseEvent.CLICK,         _onAddedToStage);
//
_xmlSocket.addEventListener(Event.CONNECT,          _onConnected);
_xmlSocket.addEventListener(IOErrorEvent.IO_ERROR,  _onIOError);
 
}
//ソケットの接続
private function _onConnected(aEvent : Event):void
{
//  TRACE
trace("onConnect() aEvent: " + aEvent);
 
//  EVENTS
_xmlSocket.removeEventListener(Event.CONNECT,           _onConnected);
_xmlSocket.removeEventListener(IOErrorEvent.IO_ERROR,   _onIOError);
//
_xmlSocket.addEventListener(DataEvent.DATA,             _onDataReceived);
_xmlSocket.addEventListener(Event.CLOSE,                _onSocketClose);

button_send.addEventListener(MouseEvent.CLICK, _onMessageSend);
 
}
 
//ソケットの切断
private function _onSocketClose(aEvent : Event):void
{
//  TRACE
trace("_onSocketClose aEvent : " + aEvent);
 
//  EVENTS
_xmlSocket.removeEventListener(Event.CLOSE, _onSocketClose);
_xmlSocket.removeEventListener(DataEvent.DATA, _onDataReceived);
}

//送信ボタンが押されたときの処理
private function _onMessageSend(evt:MouseEvent):void
{
_xmlSocket.send(message_send.text);
}
 
//送信データの受信
private function _onDataReceived(aEvent : DataEvent):void
{
try {
 
//  Show the server data in text
message_receive.htmlText += (aEvent.data + "\n");
 
//scroll down to show latest line
message_receive.scrollV = message_receive.maxScrollV;
 
} catch (error : Error) {
//  TRACE
trace("_onDataReceived error:  " + error);
}
}
 
//接続エラー
private function _onIOError(aEvent : IOErrorEvent):void
{
//  TRACE
trace("_onIOError aEvent: " + aEvent);
 
//  EVENTS
_xmlSocket.removeEventListener(Event.CONNECT, _onConnected);
_xmlSocket.removeEventListener(IOErrorEvent.IO_ERROR, _onIOError);
stage.addEventListener(MouseEvent.CLICK, _onAddedToStage);
}
 
}
}


Node.jsが入っていることが前提としてサーバー側のソースが以下の通り

app.js

// --------------------------------------
// Imports
// --------------------------------------
var net = require('net');
var mySocket;

// --------------------------------------
// Construct The Socket
// --------------------------------------
// create the server and register event listeners
var server = net.createServer(function(socket) {
    mySocket = socket;
    mySocket.on("connect", onConnect);
    mySocket.on("data", onData);
});

// --------------------------------------
// Events
// --------------------------------------
/**
 * Handles the Event: <code>"connect"</code>.
 *
*/
function onConnect()
{
    console.log("Connected to Flash");
}

/**
 * Handles the Event: <code>"data"</code>.
 *
 * When flash sends us data, this method will handle it
 *
*/
function onData(d)
{
    if(d == "exit\0")
    {
        console.log("exit");
        mySocket.end();
        server.close();
    }
    else
    {
        console.log("From Flash = " + d);
        mySocket.write(d, 'utf8');
    }
}

// --------------------------------------
// Start the Socket
// --------------------------------------
server.listen(3000, "IPアドレス");


実行
























という具合で動きました。

0 件のコメント:

コメントを投稿