개발/플래시
[개발메모] Flash9 AS3로 FMS2서버에 연결하는 방법
MOBIUS!
2007. 7. 7. 14:41
Flash9과 ActionScript 3.0을 사용한 Flash Media Server에 연결하는 방법을 메모해둡니다
일단 빨간색으로 칠해진부분 주목
오브젝트 인코딩을 정해주지 않으면 서버에 제대로 연결을 못하는 사태가 발생합니다
또한 서버로부터 응답을 받는 부분도 만들어주어야겠지요
package
{
import flash.display.MovieClip
import flash.events.AsyncErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.IOErrorEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
{
import flash.display.MovieClip
import flash.events.AsyncErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.IOErrorEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
public class FMS2Connector extends MovieClip
{
private var connection:NetConnection;
private var stream:NetStream;
public function FMS2Connector():void
{
// netconnection
connection = new NetConnection();
connection.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.objectEncoding = ObjectEncoding.AMF0;
// netconnection client
var netconnectionclient:Object = new Object();
netconnectionclient.msgFromSrvr = msgFromSrvr;
connection.client = netconnectionclient;
try
{
connection.connect("rtmp://localhost:1935/testApp/test");
//connection.connect(null);
}
catch (error:ArgumentError)
{
trace(error.message);
}
}
public function init()
{
// netstream
stream = new NetStream(connection);
stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
}
public function msgFromSrvr(info:String):void
{
Debug.trace2("-----------------------");
Debug.trace2("message from server:");
Debug.trace2(info);
}
//*****************************************************
// event handler
private function netStatusHandler(event:NetStatusEvent):void
{
trace("-----------------------");
trace("netStatusHandler:");
trace(event.info.code);
for (var _i in event.info)
{
trace(_i+':'+event.info[_i]);
}
switch (event.info.code)
{
case "NetConnection.Connect.Success":
trace("connect successful");
init();
break;
}
}
//*****************************************************
// error handler
private function ioErrorHandler(event:IOErrorEvent):void
{
trace("-----------------------");
trace("ioErrorHandler: " + event);
trace(event.text);
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("-----------------------");
trace("securityErrorHandler: " + event);
trace(event.text);
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("-----------------------");
trace("asyncErrorHandler: " + event);
trace(event.text + "\n" + event.error);
}
}
}
{
private var connection:NetConnection;
private var stream:NetStream;
public function FMS2Connector():void
{
// netconnection
connection = new NetConnection();
connection.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.objectEncoding = ObjectEncoding.AMF0;
// netconnection client
var netconnectionclient:Object = new Object();
netconnectionclient.msgFromSrvr = msgFromSrvr;
connection.client = netconnectionclient;
try
{
connection.connect("rtmp://localhost:1935/testApp/test");
//connection.connect(null);
}
catch (error:ArgumentError)
{
trace(error.message);
}
}
public function init()
{
// netstream
stream = new NetStream(connection);
stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
}
public function msgFromSrvr(info:String):void
{
Debug.trace2("-----------------------");
Debug.trace2("message from server:");
Debug.trace2(info);
}
//*****************************************************
// event handler
private function netStatusHandler(event:NetStatusEvent):void
{
trace("-----------------------");
trace("netStatusHandler:");
trace(event.info.code);
for (var _i in event.info)
{
trace(_i+':'+event.info[_i]);
}
switch (event.info.code)
{
case "NetConnection.Connect.Success":
trace("connect successful");
init();
break;
}
}
//*****************************************************
// error handler
private function ioErrorHandler(event:IOErrorEvent):void
{
trace("-----------------------");
trace("ioErrorHandler: " + event);
trace(event.text);
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("-----------------------");
trace("securityErrorHandler: " + event);
trace(event.text);
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("-----------------------");
trace("asyncErrorHandler: " + event);
trace(event.text + "\n" + event.error);
}
}
}
Server Side의 testApp.asc 혹은 testApp/main.asc 파일로 존재해야하는 스크립트입니다
일단 application.acceptConnection 로 서버로부터 들어오는 연결요청을 허락해주고
newClient.call 로 다시 클라이언트에게 메세지를 뿌려줍니다
//load("components.asc");
application.onConnect = function(newClient)
{
application.acceptConnection(newClient);
var msg = "connect successful";
trace("Sending this message: " + msg);
newClient.call("msgFromSrvr", false, msg);
}
application.onConnect = function(newClient)
{
application.acceptConnection(newClient);
var msg = "connect successful";
trace("Sending this message: " + msg);
newClient.call("msgFromSrvr", false, msg);
}
일단 오브젝트 인코딩을 정해주지 않으면 연결이 되지 않으니 주의해야겠습니다.