Help me demo Chat Flash client !!!!

tuananh4689
edited March 2014 in Flash (deprecated)
Dear Every Body,

I use Flash Develop and Flash Professional

According to your code, I can connect to GameServer . In next, would you like to help me a example to send and receive message after creating and joining my room

Script for me:

package control.PhotonServer
{
import control.Const;
import control.GameConstants;
import exitgames.photon.events.CustomEvent;
import exitgames.photon.events.JoinEvent;
import exitgames.photon.events.LeaveEvent;
import exitgames.photon.events.PhotonErrorEvent;
import exitgames.photon.loadbalancing.Actor;
import exitgames.photon.loadbalancing.LoadBalancingClient;
import exitgames.photon.PhotonCore;
import exitgames.photon.PhotonPeer;
import exitgames.photon.response.CustomResponse;
import exitgames.photon.response.InitializeConnectionResponse;
import exitgames.photon.response.JoinResponse;
import exitgames.photon.response.LeaveResponse;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.utils.Dictionary;
import flash.utils.Timer;

public class CoreApi extends EventDispatcher
{
private var _peer:LoadBalancingClient;
private var _photon:PhotonCore;
private const SERVER:String = "app.exitgamescloud.com:4530";
private const PORT:int = 4530;
private const POLICYPORT:int = 843;
private const APPLICATIONID:String = "9b04ba08-d5cb-412f-9888-b508c1bf764f"; //use your own app id here
private const APPLICATIONVERSION:String = "v1.0";
private var _timer:Timer;
private var _countConnect:int = 5;
private var _timeConnect:int = 1000;
private var options:Object;


public function CoreApi()
{

}

public function connectServer():void
{

if (!_peer)
{
options = new Object();
options.keepMasterConnection = false;
options.lobbyName = "Sin City";
options.lobbyStats = true;
options.isVisible = true;
options.maxPlayers = 5;
options.emptyRoomLiveTime = 1000;
options.isOpen = true;
options.keepMasterConnection = true;

_photon = new PhotonCore();
addEventPhoton();

_peer = new LoadBalancingClient(SERVER, APPLICATIONID, APPLICATIONVERSION);
_peer.connect(options)



_timer = new Timer(_timeConnect, _countConnect);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimeConnectServer);
_timer.start();

}

}

private function addEventPhoton():void
{
//listen for photon responses
_photon.addEventListener(InitializeConnectionResponse.TYPE, onPhotonResponse);
_photon.addEventListener(JoinResponse.TYPE, onPhotonResponse);
_photon.addEventListener(LeaveResponse.TYPE, onPhotonResponse);
_photon.addEventListener(CustomResponse.TYPE, onPhotonResponse);

//listen for photon events
_photon.addEventListener(Event.CLOSE, onPhotonEvent);
_photon.addEventListener(JoinEvent.TYPE, onPhotonEvent);
_photon.addEventListener(LeaveEvent.TYPE, onPhotonEvent);
_photon.addEventListener(CustomEvent.TYPE, onPhotonEvent);
//_photon.addEventListener(ChatEvent.TYPE, onPhotonEvent);

//listen for errors
_photon.addEventListener(IOErrorEvent.IO_ERROR, onPhotonError);
_photon.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onPhotonError);
_photon.addEventListener(PhotonErrorEvent.ERROR, onPhotonError);
}

private function onPhotonResponse(e:InitializeConnectionResponse):void
{
trace("type of event1: " + e.type);
}

private function onPhotonEvent(e:Event):void
{
trace("type of event2: " + e.type);
}

private function onPhotonError(e:PhotonErrorEvent):void
{
trace("type of event3: " + e.type);
}

private function onTimeConnectServer(e:TimerEvent):void
{
trace("aaaaaaaaaa")
if (_peer.isInLobby())
{
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimeConnectServer);
_timer.stop();

if (_peer != null)
{
_peer.myActor().setName("tuan anh");
_peer.myActor().actorNr=1;
}

var count:int = 0;
if (_peer.availableRooms().length > 0)
{
trace("_peer.availableRooms", _peer.availableRooms()[0].playerCount);
}
count= 0;
for (var p: * in _peer.myRoomActors() ) {
++count;
}
_peer.createRoom("room1",options);


trace("Server : ", _peer.myRoom().address);
trace("Players : ", _peer.myActor().getRoom().maxPlayers ,"/",count);
trace("ROOM : ", _peer.myActor().getRoom().name);
trace("Actor Name : ", _peer.myActor().name);
trace("Actor Num : ", _peer.myActor().actorNr);
trace("Actor Local : ", _peer.myActor().isLocal);
trace("Actor in Room : ", _peer.myActor().getRoom().name);


};
}


}

}


And this trace from Flash develop:

[info] LoadBalancingClient: State: Uninitialized -> ConnectingToMasterserver
[info] LoadBalancingClient: Connecting to Master app.exitgamescloud.com:4530
[info] Master: Connected
[info] Master: Authenticate...
[info] Master: Authenticated
[info] LoadBalancingClient: State: ConnectingToMasterserver -> ConnectedToMaster
[info] Master: Join Lobby Sin City undefined ...
[info] Master: Joined to Lobby
[info] LoadBalancingClient: State: ConnectedToMaster -> JoinedLobby
aaaaaaaaaa
[info] Master: Create Room Sin City undefined ...
Server :
Players : 5 / 1
ROOM : room1
Actor Name : tuan anh
Actor Num : 1
Actor Local : true
Actor in Room : room1
[info] LoadBalancingClient: Connecting to Game 85.17.196.159:4531
[info] LoadBalancingClient: State: JoinedLobby -> ConnectingToGameserver
[info] Game: Connected
[info] Game: Authenticate...
[info] Game: Authenticated
[info] Game: Connected
[info] Game: Create Room Sin City undefined ...
[info] LoadBalancingClient: State: ConnectingToGameserver -> ConnectedToGameserver
[info] Game: myActor:
[info] LoadBalancingClient: State: ConnectedToGameserver -> Joined

Comments

  • Looks like you have seen already that sample. Tell me please if something not clear with it.
    'sendMessage' methods sends messages. Use it anytime while you are joined.
    'onEvent' called every time someone else sends message. So you receive messages here.
  • Hi vadim

    I have followed your guidance on : viewtopic.php?f=9&t=3800#p15654.
    But in event createCharRoom():

    public function createCharRoom(): void {
    var id:String = "myGame_" + availableRooms().length;
    var options:Object = new Object();
    var customGameProperties:Object = new Object();
    customGameProperties.des = String("aaaaaaasdasdasdas");

    options.maxPlayers = 4;
    options.customGameProperties = customGameProperties;
    createRoom(id, options);
    }

    and when I receive returned data, , I get unknown customGameProperties in this.availableRooms();

    override protected function onRoomList(rooms: Vector.<RoomInfo>): void {
    var objRoomList:Vector.<RoomInfo> = this.availableRooms();
    }

    Can you help me get customGameProperties.des ?

    Thanks vadim
  • You don't need availableRooms() method after you called createRoom() since availableRooms() designed for use while you are in lobby. For access properties of currently joined (including created by you) room use myRoom().getCustomProperty("des")
  • Thanks Vadim ,I have get customGameProperties.des. When i connected photon server via LoadBalancingClient through the stages

    [info] Master: Authenticated
    [info] LoadBalancingClient: State: ConnectingToMasterserver -> ConnectedToMaster
    [info] Master: Join Lobby undefined undefined ...
    [info] Master: Joined to Lobby
    [info] LoadBalancingClient: State: ConnectedToMaster -> JoinedLobby
    [info] LoadBalancingClient: Connecting to Game 85.17.196.133:4531
    [info] LoadBalancingClient: State: JoinedLobby -> ConnectingToGameserver
    [info] Master: Disconnected
    [info] Game: Connected
    [info] Game: Authenticate...
    [info] Game: Authenticated
    [info] Game: Connected
    [info] Game: Create Room undefined undefined ...
    [info] LoadBalancingClient: State: ConnectingToGameserver -> ConnectedToGameserver
    [info] Game: myActor:
    [info] LoadBalancingClient: State: ConnectedToGameserver -> Joined

    what period should I create login game ?
  • Hey Vadim , give me your skype nick name for easy exchange
  • After client gets in 'Joined' state it can send and receive events or set properties of current room or local player. Not sure if mentioned features can help 'create login game' since i did not quite get what does it mean. Can you explain details?
  • Hey vadim!

    How to do get room list after process : ConnectedToMaster -> JoinedLobby
  • Hi
    To get list of currently available rooms use client.availableRooms() method.
    Alternatively you can override client.onRoomList and client.onRoomListUpdate to handle receiving new list event (usually on connect to lobby) and subsequent room lists updates (happen while you are in lobby and room created or removed or properties of any room changed). This approach allows keep your structures (room menu for instance) up to date automatically, without periodic checking available rooms.
  • hey vadim after complete ConnectedToGameserver -> Joined

    + myRoomActors() always update status
    + myRoom() not update status ----> why? Can you help me?
  • Hi,
    What you mean by 'update status'?
  • Sorry , I did wrong function, should setCustomProperty instead of getCustomProperty;

    Another thing I want your help, I wanna add a function in Authenticate process. I've tried to trace as bellow code but it doesn't work as expected. Hope you help me soon. Thanks.

    [code2=as3]override protected function onStateChange(state: int): void {
    case LoadBalancingClient.State.Authenticate:
    trace("PhotonServer onStateChange", "Authenticate ");
    break;
    }[/code2]
  • LoadBalancingClient does not have 'Authenticate' state.
    You can control authentication only by calling LoadBalancingClient.setCustomAuthentication(...) method before connect and proper authentication setup for your app in dashboard.
  • Hey vadim

    How use propsListedInLobby in CreateRoom().I use the following:

    [code2=as3]public function createGameRoom(): void {
    var options:Object = {};
    var propsListedInLobby:Array = [];
    var _id:int = 100;

    propsListedInLobby.push("roomDes1");
    propsListedInLobby.push("1000");

    options.propsListedInLobby = propsListedInLobby;
    createRoom(_id, options);

    }

    override protected function onRoomList(rooms: Vector.<RoomInfo>):void {
    trace("Photon onRoomList", rooms);
    }[/code2]

    rooms in onRoomList() not properties propsListedInLobby

    Can you help me?
  • Hey vadim , you can example setCustomAuthentication and getCustomAuthentication in Authenticate of LoadBalancingClient!!!
  • How use propsListedInLobby in CreateRoom()
    1. Set properties
    2. Optionally mark them as 'listed in lobby'

    [code2=as3]var options:Object = {};

    var props:Object = {};
    props["roomDes1"] = "1000"

    options.customGameProperties = props
    options.propsListedInLobby = ["roomDes1"];

    createRoom(_id, options);[/code2]
  • Hey vadim , you can example setCustomAuthentication and getCustomAuthentication in Authenticate of LoadBalancingClient!!!
    There is no 'getCustomAuthentication' in LoadBalancingClient.
    Call setCustomAuthentication(authParameters: String, authType: int/loadbalancing.Constants.CustomAuthenticationType) before starting connection to set authentication parameters.
    Refer Custom Authentication documentation for details.
  • hey vadim

    How to set Authenticate after JoinedLobby and before ConnectedToGameserver in LoadBalancingClient ?

    Thank vadim!
  • hey vadim!!!

    When LoadBalancingClient.setCustomAuthentication() before LoadBalancingClient.connect()

    How to receive authencate operation response ???

    and Can you give me your skype ^^
  • Flash client calls Authenticate operation automatically. Call setCustomAuthentication(...) before initial connect. If authentication fails, onError handler with PeerErrorCode.MasterAuthenticationFailed called. Or usual workflow goes on if authentication is ok.