SmartfoxServer的客户端逻辑处理模版

来源:互联网 发布:淘宝旅行服务热线 编辑:程序博客网 时间:2024/05/25 23:27

A: 代码模板

介绍

这篇文章大家可以了解一下代码模板,使用该模板我们可以开发很多基于SmartFoxServer的应用。模板包含了发起连接,认证,加入房间,等等基本的方法。和服务器端的相应.

如果您已经学习了 "连接" (5.1) 和 "简单聊天 (5.2) ,请接着往下看。

» 基本步骤

1) 发起socket连接:这是第一步. 就像是在敲服务器的门。如果服务器开着就会相应你,并激活一个连接。

2) 登入: 在这一阶段,你要告诉服务器你是谁你想和哪个应用交互,如果操作成功的话,你就可以当前zone里的房间列表

3) 加入房间: 一旦进入了房间,你就可以和其他用户和应用的逻辑进行交互。

»代码

这里是应用-模板的代码,在源文件中有详细的注释。

import it.gotoandplay.smartfoxserver.*stop()var ip:String= "127.0.0.1"var port:Number = 9339var zone:String = "simpleChat"// Load the policy file from the serverSystem.security.loadPolicyFile("xmlsocket://" + ip + ":" + port)var smartfox:SmartFoxClient = new SmartFoxClient()smartfox.onConnection = handleConnectionsmartfox.connect(ip, port)function handleConnection(success){        if (success)        {                trace(">> Connection successfull")                sendLogin()        }        else        {                trace("[ ERROR ]: Can't connect!")        }}function sendLogin(){        smartfox.login(zone, "")}smartfox.onLogin = function(resObj:Object){        if (resObj.success)        {                // Login Successfull                trace(">> Login successfull. Nickname: " + resObj.name)        }        else        {                trace("[ ERROR ]: Login error. Server said: " + resObj.error)        }}smartfox.onRoomListUpdate = function(roomList:Object){        trace(">> Received the list of rooms\n")        trace("\tRoom List:")        trace("\t--------------------------------------------")                // Show the list of rooms        for (var i in roomList)        {                trace("\t" + roomList[i].getName())        }                // a blank line        trace("")                // Join the default room        this.autoJoin()}smartfox.onJoinRoom = function(roomObj:Room){        trace(">> Room: '" + roomObj.getName() + "' joined successfully")}smartfox.onJoinRoomError = function(errorMsg){        trace("[ ERROR ]: Error joining the room. Server said: " + errorMsg)}smartfox.onConnectionLost = function(){        trace("[ ERROR ]: Connection with server was lost")}

在代码中实现3步骤:

1)发起 socket 连接:调用 connect()和处理 onConnection() 方法.

2) 登入:调用sendLogin()方法发送一个zone名和空的用户名. 用户为以游客的身份登入. 使用onLogin() 来处理登录的相应

提示: 我们可以在服务器端设置登录处理。模板里是为了方便才使用游客登录方式

3) 加入房间: 调用join() 和 autoJoin()加入房间。并处理onJoinRoom() /on JoinRoomError()

» 跳幁

大家会发现我们的代码都在第一幁,可能有些开发者喜欢把代码写在不同的幁。

如果您要写在不同的幁上,请注意您的请求和相应要在同一幁里。不然的话可能会出问题。比如:您在第一幁发起了连接,那么处理连接事件也要在该幁。

» 总结

一旦以上的三步都成功执行了,我们就可以开始交互游戏逻辑了。


B: Flex 2.0代码模板

介绍


Flex Builder 2的模板包含两部分:SFSTemplate.mxml文件和main.as文件

参考章节 "Connection" (5.1) 和 "Simple Chat" (5.2)

» 基本步骤

1) 发起socket连接:这是第一步. 就像是在敲服务器的门。如果服务器开着就会相应你,并激活一个连接。

2) 登入: 在这一阶段,你要告诉服务器你是谁你想和哪个应用交互,如果操作成功的话,你就可以当前zone里的房间列表

3) 加入房间: 一旦进入了房间,你就可以和其他用户和应用的逻辑进行交互。

» 代码

mxml代码

SFSTemplate.mxml:

 <?xml version="1.0" encoding="utf-8"?>   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    layout="absolute"    creationComplete="main()">  <mx:Script source="main.as" />    <mx:VBox top="20" bottom="20" left="20" right="20" width="100%" height="100%">    <mx:Label text="SmartFoxServer Actionscript 3.0 client side template"    fontWeight="bold"    fontSize="12"    color="#000000" />    <mx:TextArea id="ta_debug" width="100%" height="100%" />    <mx:Spacer height="10" />  <mx:HBox width="100%" horizontalAlign="center">  <mx:Button id="bt_connect"    label="CONNECT"    width="200"    click="bt_connect_click(event)"    toolTip="Start the connection with the server" />  </mx:HBox>    </mx:VBox> </mx:Application>

as代码
main.as:

import it.gotoandplay.smartfoxserver.SmartFoxClient;import it.gotoandplay.smartfoxserver.SFSEvent;import flash.events.Event;private const NEWLINE:String = "\n";private var sfs:SmartFoxClient;public function main():void{sfs = new SmartFoxClient(true);// Register for SFS eventssfs.addEventListener(SFSEvent.onConnection, onConnection);sfs.addEventListener(SFSEvent.onConnectionLost, onConnectionLost);sfs.addEventListener(SFSEvent.onLogin, onLogin);sfs.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdate);sfs.addEventListener(SFSEvent.onJoinRoom, onJoinRoom);sfs.addEventListener(SFSEvent.onJoinRoomError, onJoinRoomError);// Register for generic errorssfs.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)sfs.addEventListener(IOErrorEvent.IO_ERROR, onIOError)debugTrace("Click the CONNECT button to start");}public function bt_connect_click(evt:Event):void{if (!sfs.isConnected)sfs.connect("127.0.0.1");elsedebugTrace("You are already connected!");}public function onConnection(evt:SFSEvent):void{var success:Boolean = evt.params.success;if (success){debugTrace("Connection successfull!");// Attempt to log in "simpleChat" Zone as a guest usersfs.login("simpleChat", "", "");}else{debugTrace("Connection failed!");}}public function onConnectionLost(evt:SFSEvent):void{debugTrace("Connection lost!");}public function onLogin(evt:SFSEvent):void{if (evt.params.success){debugTrace("Successfully logged in");}else{debugTrace("Login failed. Reason: " + evt.params.error);}}public function onRoomListUpdate(evt:SFSEvent):void{debugTrace("Room list received");// Tell the server to auto-join us in the default room for this Zonesfs.autoJoin();}public function onJoinRoom(evt:SFSEvent):void{debugTrace("Successfully joined room: " + evt.params.room.getName());}public function onJoinRoomError(evt:SFSEvent):void{debugTrace("Problems joining default room. Reason: " + evt.params.error);}public function onSecurityError(evt:SecurityErrorEvent):void{debugTrace("Security error: " + evt.text);}public function onIOError(evt:IOErrorEvent):void{debugTrace("I/O Error: " + evt.text);}public function debugTrace(msg:String):void{ta_debug.text += "--> " + msg + NEWLINE;}

在代码中实现3步骤:

1)发起 socket 连接:调用 connect()和处理 onConnection() 方法.

2) 登入:调用sendLogin()方法发送一个zone名和空的用户名. 用户为以游客的身份登入. 使用onLogin() 来处理登录的相应

提示: 我们可以在服务器端设置登录处理。模板里是为了方便才使用游客登录方式

3) 加入房间: 调用join() 和 autoJoin()加入房间。并处理onJoinRoom() /on JoinRoomError()

» 处理错误

注册 IOErrorEventSecurityErrorEvent处理方法来处理I/O错误和沙箱安全. 


0 0
原创粉丝点击