unity about buddy

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

netstabber
Posts: 11
Joined: 06 Jan 2010, 09:55
Contact:

unity about buddy

Postby netstabber » 12 Aug 2011, 02:57

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Entities.Managers;
using Sfs2X.Entities.Variables;
using Sfs2X.Requests;
using Sfs2X.Logging;
using Sfs2X.Util;



public class BuddyMessenger : MonoBehaviour
{
public string serverName = "127.0.0.1";
public int serverPort = 9933;

private SmartFox smartFox;
private string zone = "SimpleChat";
private string username = "";
private string password = "";
private string loginErrorMessage = "";
private bool isLoggedIn;
private bool isJoining = false;


private string newMessage = "";
private ArrayList messages = new ArrayList();
// Locker to use for messages collection to ensure its cross-thread safety
private System.Object messagesLocker = new System.Object();

private Vector2 chatScrollPosition, userScrollPosition;

private int roomSelection = 0;
private string[] roomStrings;

public GUISkin gSkin;

private Room currentActiveRoom;
private Boolean _isBuddyListInited;

void Start()
{
bool debug = true;
if (SmartFoxConnection.IsInitialized)
{
smartFox = SmartFoxConnection.Connection;
}
else
{
smartFox = new SmartFox(debug);
}

// Register callback delegate
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
smartFox.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);

smartFox.AddEventListener(SFSBuddyEvent.BUDDY_LIST_INIT, OnBuddyListInit);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_ERROR, OnBuddyError);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_ONLINE_STATE_UPDATE, OnBuddyListUpdate);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_VARIABLES_UPDATE, OnBuddyListUpdate);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_ADD, OnBuddyListUpdate);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_REMOVE, OnBuddyListUpdate);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_BLOCK, OnBuddyListUpdate);
smartFox.AddEventListener(SFSBuddyEvent.BUDDY_MESSAGE, OnBuddyMessage);

smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage);

smartFox.Connect(serverName, serverPort);

Debug.Log(Application.platform.ToString());
isBuddyListInited = false;
}

void FixedUpdate()
{
smartFox.ProcessEvents();
}

private void UnregisterSFSSceneCallbacks()
{
// This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
smartFox.RemoveAllEventListeners();
}

public void onBuddyMessage(SFSBuddyEvent evt)
{
Boolean isItMe = (string)evt.Params["isItMe"];
Buddy sender = (string)evt.Params["buddy"];
String message = (string)evt.Params["message"];

Buddy buddy;

if (isItMe)
{
String buddyName = ((string)evt.Params["data"] as ISFSObject).getUtfString("recipient");
buddy = smartFox.buddyManager.getBuddyByName(buddyName);
}
else
buddy = sender;

if (buddy != null)
{
ChatTab tab = addChatTab(buddy, false);
tab.displayMessage("<b>" + (isItMe ? "You" : tab.getDisplayedName()) + ":</b> " + message);
}
}

public void OnBuddyListUpdate(SFSBuddyEvent evt)
{
ArrayCollection buddies = new ArrayCollection();

foreach (Buddy buddy in smartFox.buddyManager.buddyList)
{
buddies.addItem(buddy);

// Refresh the buddy chat tab (if open) so that it matches the buddy state
ChatTab tab = stn_chats.getChildByName(buddy.name) as ChatTab;
if (tab != null)
{
tab.buddy = buddy;
tab.refresh();

// If a buddy was blocked, close its tab
if (buddy.isBlocked)
stn_chats.removeChild(tab);
}
}

ls_buddies.dataProvider = buddies;
}

public void OnBuddyError(SFSBuddyEvent evt)
{

Debug.Log("The following error occurred in the buddy list system: " + (string)evt.Params["errorMessage"]);
}

public void OnBuddyListInit(SFSBuddyEvent evt)
{
// Populate list of buddies
OnBuddyListUpdate(evt);

// Set current user details as buddy

// Nick
ti_nick.text = smartFox.buddyManager.myNickName;

// States
Array states = smartFox.buddyManager.buddyStates;
dd_states.dataProvider = states;
String state = (smartFox.buddyManager.myState != null ? smartFox.buddyManager.myState : "");
if (states.indexOf(state) > -1)
dd_states.selectedIndex = states.indexOf(state);
else
dd_states.selectedIndex = 0;

// Online
cb_online.selected = smartFox.buddyManager.myOnlineState;

// Buddy variables
BuddyVariable age = smartFox.buddyManager.getMyVariable(BUDDYVAR_AGE);
ns_age.value = ((age != null && !age.isNull()) ? age.getIntValue() : 30);

BuddyVariable mood = smartFox.buddyManager.getMyVariable(BUDDYVAR_MOOD);
ti_mood.text = ((mood != null && !mood.isNull()) ? mood.getStringValue() : "");

isBuddyListInited = true;
}

public void OnConnection(BaseEvent evt)
{
bool success = (bool)evt.Params["success"];
string error = (string)evt.Params["errorMessage"];

Debug.Log("On Connection callback got: " + success + " (error : <" + error + ">)");

if (success)
{
SmartFoxConnection.Connection = smartFox;
}
}

public void OnConnectionLost(BaseEvent evt)
{
Debug.Log("OnConnectionLost");
isLoggedIn = false;
isJoining = false;
currentActiveRoom = null;
UnregisterSFSSceneCallbacks();
}

// Various SFS callbacks
public void OnLogin(BaseEvent evt)
{
try
{
// bool success = true;
if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"])
{
loginErrorMessage = (string)evt.Params["errorMessage"];
Debug.Log("Login error: " + loginErrorMessage);
}
else
{
isLoggedIn = true;
Debug.Log("Logged in successfully");
ReadRoomListAndJoin();
smartFox.Send(new InitBuddyListRequest());
}
}
catch (Exception ex)
{
Debug.Log("Exception handling login request: " + ex.Message + " " + ex.StackTrace);
}
}

public void OnLoginError(BaseEvent evt)
{
Debug.Log("Login error: " + (string)evt.Params["errorMessage"]);
}

void OnLogout(BaseEvent evt)
{
Debug.Log("OnLogout");
isLoggedIn = false;
isJoining = false;
currentActiveRoom = null;
}

public void OnDebugMessage(BaseEvent evt)
{
string message = (string)evt.Params["message"];
Debug.Log("[SFS DEBUG] " + message);
}

private void ReadRoomListAndJoin()
{
Debug.Log("Room list: ");

List<Room> roomList = smartFox.RoomManager.GetRoomList();
List<string> roomNames = new List<string>();
foreach (Room room in roomList)
{
if (room.IsHidden || room.IsPasswordProtected)
{
continue;
}

roomNames.Add(room.Name);
Debug.Log("Room id: " + room.Id + " has name: " + room.Name);

}

roomStrings = roomNames.ToArray();

if (smartFox.LastJoinedRoom == null)
{
JoinRoom("The Lobby");
}
}


void JoinRoom(string roomName)
{
if (isJoining) return;

isJoining = true;
currentActiveRoom = null;
Debug.Log("Joining room: " + roomName);

// Need to leave current room, if we are joined one
if (smartFox.LastJoinedRoom == null)
smartFox.Send(new JoinRoomRequest(roomName));
else
smartFox.Send(new JoinRoomRequest(roomName, "", smartFox.LastJoinedRoom.Id));
}

void OnJoinRoom(BaseEvent evt)
{
Room room = (Room)evt.Params["room"];
Debug.Log("Room " + room.Name + " joined successfully");

lock (messagesLocker)
{
messages.Clear();
}

currentActiveRoom = room;
isJoining = false;
}

void OnPublicMessage(BaseEvent evt)
{
try
{
string message = (string)evt.Params["message"];
User sender = (User)evt.Params["sender"];

// We use lock here to ensure cross-thread safety on the messages collection
lock (messagesLocker)
{
messages.Add(sender.Name + " said " + message);
}

chatScrollPosition.y = Mathf.Infinity;
Debug.Log("User " + sender.Name + " said: " + message);
}
catch (Exception ex)
{
Debug.Log("Exception handling public message: " + ex.Message + ex.StackTrace);
}
}



// Finally draw all the lobby GUI
void OnGUI()
{
if (smartFox == null) return;
GUI.skin = gSkin;

GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo");


if (!smartFox.IsConnected)
{
GUI.Label(new Rect(10, 90, 100, 100), "Connecting...");
}
// Login
else if (!isLoggedIn)
{
GUI.Label(new Rect(10, 90, 100, 100), "Zone: ");
zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25);

GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);

GUI.Label(new Rect(10, 142, 100, 100), "Password: ");
password = GUI.TextField(new Rect(100, 142, 200, 20), password, 4);

GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);

if (GUI.Button(new Rect(100, 166, 100, 24), "Login") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
{
Debug.Log("Sending login request");
smartFox.Send(new LoginRequest(username, password, zone));
}
}
else if (isJoining)
{
// Standard view
if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
{
smartFox.Send(new LogoutRequest());
}

GUI.Label(new Rect(498, 248, 180, 40), "Joining room");
}
else if (currentActiveRoom != null)
{
// Standard view
if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
{
smartFox.Send(new LogoutRequest());
}

GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.Name);

// Room list
GUI.Box(new Rect(490, 80, 180, 170), "Room List");

GUILayout.BeginArea(new Rect(500, 110, 150, 130));
roomSelection = GUILayout.SelectionGrid(roomSelection, roomStrings, 1, "RoomListButton");

if (roomStrings[roomSelection] != currentActiveRoom.Name)
{
JoinRoom(roomStrings[roomSelection]);
GUILayout.EndArea();
return;
}

GUILayout.EndArea();

// User list
GUI.Box(new Rect(490, 270, 180, 200), "Users");

GUILayout.BeginArea(new Rect(500, 300, 150, 160));
userScrollPosition = GUILayout.BeginScrollView(userScrollPosition, GUILayout.Width(150), GUILayout.Height(160));
GUILayout.BeginVertical();

foreach (User user in currentActiveRoom.UserList)
{
GUILayout.Label(user.Name);
}

GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndArea();



// Chat history
GUI.Box(new Rect(10, 80, 470, 390), "Chat");

GUILayout.BeginArea(new Rect(20, 110, 450, 350));
chatScrollPosition = GUILayout.BeginScrollView(chatScrollPosition, GUILayout.Width(450), GUILayout.Height(350));
GUILayout.BeginVertical();

// We use lock here to ensure cross-thread safety on the messages collection
lock (messagesLocker)
{
foreach (string message in messages)
{
GUILayout.Label(message);
}
}

GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndArea();

// Send message
newMessage = GUI.TextField(new Rect(10, 480, 370, 20), newMessage, 50);
if (GUI.Button(new Rect(390, 478, 90, 24), "Send") || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
{
smartFox.Send(new PublicMessageRequest(newMessage));
newMessage = "";
}

}
}
}

the problem it"Assets/Game/Scripts/BuddyMessenger.cs(69,18): error CS0123: A method or delegate `BuddyMessenger.OnBuddyListInit(Sfs2X.Core.SFSBuddyEvent)' parameters do not match delegate `Sfs2X.Core.EventListenerDelegate(Sfs2X.Core.BaseEvent)' parameters"
I make the"SFS2X_AS3_Examples\Flex\BuddyMessenger"to c# but I don't why please help me thank you
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 12 Aug 2011, 17:12

The error says that :

Code: Select all

public void OnBuddyListInit(SFSBuddyEvent evt)


should be

Code: Select all

public void OnBuddyListInit(BaseEvent evt)
[/code]

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 18 guests