Main Menu  

   

Login Form  

   

(005) Sending a Public Message

Details
SFS_Connect.cs
using UnityEngine;
using System.Collections;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Requests;
using Sfs2X.Entities;

public class SFS2X_Connect : MonoBehaviour {

	public string ServerIP = "127.0.0.1";
	public int ServerPort = 9933;
	public string ZoneName = "BasicExamples";
	public string UserName = "";
	public string RoomName = "The Lobby";
	
	SmartFox sfs;

	void Start()
	{
		sfs = new SmartFox();
		sfs.ThreadSafeMode = true;
		
		sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
		sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
		sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
		sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
		sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
		sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
		
		sfs.Connect(ServerIP, ServerPort);
	}
	
	void OnLogin(BaseEvent e)
	{
		Debug.Log("Logged In: " + e.Params["user"]);
		sfs.Send(new JoinRoomRequest(RoomName));
	}
	
	void OnJoinRoom(BaseEvent e)
	{
		Debug.Log("Joined Room: " + e.Params["room"]);
		sfs.Send(new PublicMessageRequest("Hello World!"));
	}
	
	void OnPublicMessage(BaseEvent e)
	{
		Room room = (Room)e.Params["room"];
		User sender = (User)e.Params["sender"];
		Debug.Log("[" + room.Name + "] " + sender.Name + ": " + e.Params["message"]);
	}
	
	void OnJoinRoomError(BaseEvent e)
	{
		Debug.Log("Join Room Error (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
	}
	
	void OnLoginError(BaseEvent e)
	{
		Debug.Log("Login error: (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
	}
	
	void OnConnection(BaseEvent e)
	{
		if ((bool)e.Params["success"])
		{
			Debug.Log("Successfully Connected");
			sfs.Send(new LoginRequest(UserName, "", ZoneName));
		} else {
			Debug.Log("Connection Failed");
		}
	}
	
	void Update()
	{
		sfs.ProcessEvents();
	}
	
	void OnApplicationQuit()
	{
		if (sfs.IsConnected)
			sfs.Disconnect();
	}
}
   
© 2004-2015 Genesis Rage