Sending data without login?

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

Moderators: Lapo, Bax

Post Reply
Zerano
Posts: 10
Joined: 16 Jun 2011, 10:18

Sending data without login?

Post by Zerano »

Helle,

is it possible to send some data without login? I would like to make the registration on the client/server. the RegistrationRequest does not work here... here is my current script:

Code: Select all

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.Requests;
using Sfs2X.Logging;
using Sfs2X.Entities.Data;

public class LobbyGUI : MonoBehaviour {

	private SmartFox smartFox;
	private string zone = "SimpleChat";
	public string serverName = "127.0.0.1";
	public int serverPort = 9933;
	private string username = "";
	private string email="";
	private string userpassword="";
	private string repeatpassword="";
	private string loginErrorMessage = "";
	public int areaHeight = 195;
 	public int areaWidth = 200;
	public Texture2D background;
	private ArrayList messages = new ArrayList();
	private System.Object messagesLocker = new System.Object();
	public GUISkin gSkin;
		
	void Start()
	{
		bool debug = false;
		if (SmartFoxConnection.IsInitialized)
		{
			smartFox = SmartFoxConnection.Connection;
		}
		else
		{
			smartFox = new SmartFox(debug);
		}
		
		AddEventListeners();
		smartFox.Connect(serverName, serverPort);
			
		smartFox.AddLogListener(LogLevel.INFO, OnDebugMessage);
	}
	
	void FixedUpdate() {
		smartFox.ProcessEvents();
	}
	
	#region Callbacks
	
	private void AddEventListeners() {
		
		smartFox.RemoveAllEventListeners();
		smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
		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.PUBLIC_MESSAGE, OnPublicMessage);
		
		smartFox.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnCreateRoomError);
		
		smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
		smartFox.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEnterRoom);
		smartFox.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserLeaveRoom);
		smartFox.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdded);
		smartFox.AddEventListener(SFSEvent.ROOM_REMOVE, OnRoomDeleted);
		smartFox.AddEventListener(SFSEvent.USER_COUNT_CHANGE, OnUserCountChange);
		smartFox.AddEventListener(SFSEvent.UDP_INIT, OnUdpInit);
	}
	
	private void UnregisterSFSSceneCallbacks() {
		smartFox.RemoveAllEventListeners();
	}
	
	private void OnExtensionResponse(BaseEvent evt) {
		try {
			string cmd = (string)evt.Params["cmd"];
			ISFSObject dt = (SFSObject)evt.Params["params"];
			
			}
		catch (Exception e) {
			Debug.Log("Exception handling response: "+e.Message+" >>> "+e.StackTrace);
		}
		
	}	
	
	private void RegistrationRequest(String name, String password, string email){
		Room room = smartFox.LastJoinedRoom;
		ISFSObject data = new SFSObject();
		data.PutUtfString("name",name);
		data.PutUtfString("password",password);
		data.PutUtfString("email",email);
		ExtensionRequest request = new ExtensionRequest("registration", data, room);
		smartFox.Send(request);
	}
	
	public void OnConnection(BaseEvent evt) {
		bool success = (bool)evt.Params["success"];
		if (success) {
			SmartFoxConnection.Connection = smartFox;
			Debug.Log("Connected...");
		}
	}

	public void OnConnectionLost(BaseEvent evt) {
		UnregisterSFSSceneCallbacks();
	}

	public void OnLogin(BaseEvent evt) {
		try {
			if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) {
				loginErrorMessage = (string)evt.Params["errorMessage"];
				Debug.Log("Login error: "+loginErrorMessage);
			}
			else {
				Debug.Log("Logged in successfully");
				// Startup up UDP
				smartFox.InitUDP(serverName, serverPort);
			}
		}
		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"]);
		msg="Wrong Username or Password";
		menuLevel=3;
	}
	
	public void OnUdpInit(BaseEvent evt) {
		if (evt.Params.ContainsKey("success") && !(bool)evt.Params["success"]) {
			loginErrorMessage = (string)evt.Params["errorMessage"];
			Debug.Log("UDP error: "+loginErrorMessage);
		} else {
			Debug.Log("UDP ok");
			SetupRoom("Sunna");
		}
	}
	
	void OnLogout(BaseEvent evt) {
		smartFox.Disconnect();
	}
	
	public void OnDebugMessage(BaseEvent evt) {
		string message = (string)evt.Params["message"];
		Debug.Log("[SFS DEBUG] " + message);
	}

	public void OnJoinRoom(BaseEvent evt)
	{
		Room room = (Room)evt.Params["room"];
		// If we joined a game room, then we either created it (and auto joined) or manually selected a game to join
		if (room.IsGame) {
			Debug.Log ("Joined game room " + room.Name);
			UnregisterSFSSceneCallbacks();
			Application.LoadLevel("Sunna");
			
		}
	}

	public void OnCreateRoomError(BaseEvent evt) {
		string error = (string)evt.Params["errorMessage"];
		Debug.Log("Room creation error; the following error occurred: " + error);
	}

	public void OnUserEnterRoom(BaseEvent evt) {
		User user = (User)evt.Params["user"];
		lock (messagesLocker) {
			messages.Add(user.Name + " joined room");
		}
	}

	private void OnUserLeaveRoom(BaseEvent evt) {
		User user = (User)evt.Params["user"];
		lock (messagesLocker) {
			messages.Add(user.Name + " left room");
		}
	}

	public void OnRoomAdded(BaseEvent evt) {
		Room room = (Room)evt.Params["room"];
		if ( room.IsGame ) {
		//	smartFox.Send(new JoinRoomRequest("Sunna", null, smartFox.LastJoinedRoom.Id));
		}
	}
	
	public void OnUserCountChange(BaseEvent evt) {
		Room room = (Room)evt.Params["room"];
		if (room.IsGame ) {
		}
	}

	public void OnRoomDeleted(BaseEvent evt) {
	}

	void OnPublicMessage(BaseEvent evt) {
		try {
			string message = (string)evt.Params["message"];
			User sender = (User)evt.Params["sender"];
	
			lock (messagesLocker) {
				messages.Add(sender.Name + " said " + message);
			}
			
			Debug.Log("User " + sender.Name + " said: " + message);
		}
		catch (Exception ex) {
			Debug.Log("Exception handling public message: "+ex.Message+ex.StackTrace);
		}
	}

	#endregion Callbacks
	
	private string msg=string.Empty;
	private int menuLevel;
	
	void OnGUI()
	{
		if (smartFox == null){
			return;
		}
	
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), background);
		GUI.skin = gSkin;
		
		int ScreenX = ((int)(Screen.width / 2) - (areaWidth / 2));
    	int ScreenY = ((int)(Screen.height / 2) - (areaHeight / 2));
		
		switch(menuLevel){
			case 0: //Login
				GUILayout.BeginArea (new Rect (ScreenX, ScreenY, areaWidth, areaHeight),"Login","Window");
			
				GUILayout.BeginHorizontal();
				GUILayout.Label("Username:",GUILayout.MaxWidth(64));
				username = GUILayout.TextField( username, 25,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
			
				GUILayout.BeginHorizontal();
				GUILayout.Label("Password:  ",GUILayout.MaxWidth(64));
				userpassword=GUILayout.PasswordField (userpassword, "*"[0], 25,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
			
				GUILayout.Space(10);
			
				GUILayout.BeginHorizontal();
				if (GUILayout.Button("Verbinden")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
				{
					smartFox.Send(new LoginRequest(username, userpassword, zone));
				}
			
				if(GUILayout.Button("Beenden")){
					Application.Quit();
				}
				GUILayout.EndHorizontal();
			
				GUILayout.Space(8);
				GUILayout.Label("Noch kein Account?");
				GUILayout.Space(6);
				GUILayout.BeginHorizontal();

				if(GUILayout.Button("",GUILayout.MaxWidth(16),GUILayout.MaxHeight(16))){
					menuLevel=1;
				}
				GUILayout.Label("Registrieren");	
				GUILayout.EndHorizontal();
			
				GUILayout.BeginHorizontal();
				if(GUILayout.Button("",GUILayout.MaxWidth(16),GUILayout.MaxHeight(16))){
					menuLevel=2;
				}
				GUILayout.Label("Password vergessen");	
				GUILayout.EndHorizontal();
			
				GUILayout.EndArea();
			break;
			case 1: //Registration
				GUILayout.BeginArea (new Rect (ScreenX, ScreenY, areaWidth, areaHeight-35),"Registration","Window");
				GUILayout.BeginHorizontal();
				GUILayout.Label("Username:",GUILayout.MaxWidth(64));
				username = GUILayout.TextField( username, 25,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
			
				GUILayout.BeginHorizontal();
				GUILayout.Label("Password:  ",GUILayout.MaxWidth(64));
				userpassword=GUILayout.PasswordField (userpassword, "*"[0], 25,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
			
				GUILayout.BeginHorizontal();
				GUILayout.Label("Password:  ",GUILayout.MaxWidth(64));
				repeatpassword=GUILayout.PasswordField (repeatpassword, "*"[0], 25,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
			
				GUILayout.BeginHorizontal();
				GUILayout.Label("E-Mail:",GUILayout.MaxWidth(64));
				email = GUILayout.TextField( email, 80,GUILayout.MaxWidth(111));
				GUILayout.EndHorizontal();
				
				GUILayout.BeginHorizontal();
				if (GUILayout.Button("Senden")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
				{
					if(username == string.Empty || userpassword == string.Empty || email == string.Empty){
						msg="Please complete all fields";
						menuLevel=3;
					}
				
					if(userpassword == repeatpassword ){
						RegistrationRequest(username,userpassword,email);
					}else{
						msg="The two passwords are not equal.";
						menuLevel=3;
					}
				}
			
				if(GUILayout.Button("Beenden")){
					menuLevel=0;
				}
				GUILayout.EndHorizontal();
				GUILayout.EndArea();
			break;
			
			case 2: //Forgot Paassword
			
			break;
			case 3: //Messages
				GUILayout.BeginArea (new Rect (ScreenX, ScreenY, areaWidth, areaHeight/2),"Login","Window");
				GUILayout.Label(msg);
				GUILayout.FlexibleSpace();
				if(GUILayout.Button("Try again") || (Event.current.type == EventType.keyDown && Event.current.character == '\n')){
					menuLevel=0;
				}
			
				GUILayout.EndArea();
			break;
			
		}
	}
	
	private void SetupRoom(string roomName){
		if(smartFox.GetRoomByName(roomName)==null){
			
			RoomSettings settings = new RoomSettings(roomName);
			settings.GroupId = "game";
			settings.IsGame = true;
			settings.MaxSpectators = 0;
			settings.Extension = new RoomExtension(NetworkManager.ExtName, NetworkManager.ExtClass);
			smartFox.Send(new CreateRoomRequest(settings, true, smartFox.LastJoinedRoom));
		}else{
			smartFox.Send(new JoinRoomRequest(roomName));
		}
	}
	
	 void OnApplicationQuit() {
        if (smartFox.IsConnected) {
            smartFox.Disconnect();
        }
    } 
}
Zerano
Posts: 10
Joined: 16 Jun 2011, 10:18

Post by Zerano »

Okay found a solution...
Nieles
Posts: 12
Joined: 25 Mar 2010, 15:07

Post by Nieles »

Zerano wrote:Okay found a solution...
Can you share your solution? :)
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Post by appels »

The client needs to be logged in to send data so you need to create a zone whithout login for that.
You can write a logic that your client connects to that zone first, send the data and then disconnects and connects to the zone with login.
I did the same for a user registration.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Did the same here as appels
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Post Reply