Page 1 of 2

sfs pro login example

Posted: 10 Sep 2010, 20:58
by Robbilie
hey guys i know its quite easy but I dont know the answer:

I used the example from the docs "sfs pro tutorial login example"

i integrated a password in the api c# script and i wrote the extension....

I get the debug that it is right but the last snippet from the tutorial is in javascript and i didnt integrated it yet cause i dont know how...

Would anybody be so kind to translate this snippet to c# for me?

Thanks :)
Robert

Posted: 11 Sep 2010, 18:25
by Robbilie

Unity OnExtensionResponse C#

Posted: 12 Sep 2010, 01:04
by CameronB
I don't know if you are using unity, but here is an example of how I'm handling OnExtensionResponse. This is for an XML response, with two parameters (two strings) embedded

I in no way claim for this to be a good method, or even a right one, but it works for me. In your particular case, this may not be what you need, but it should give you enough of the syntax to work it out on your own...

Code: Select all

   public void OnExtensionResponse(object data, string type)
   {
      // Handle XML responses
       if (type == SmartFoxClient.XTMSG_TYPE_XML)
       {
          print("XML Response Handler");
              SFSObject responseData = (SFSObject)data;
           // TODO: check command and perform required actions

         print("Response Data: " + responseData.GetString("_cmd"));
         switch ( responseData.GetString("_cmd") )
         {
            case "logOK":
               OnLogin(true, responseData.GetString("name"), responseData.GetString("err"));
               break;
            case "logKO":
               OnLogin(false, responseData.GetString("name"), responseData.GetString("err"));
               break;
         }
      }

   }


Also...

Code: Select all

   public void OnLogin(bool success, string name, string error) {

      if ( success ) {
         // Lets wait for the room list

      } else {
         // Login failed - lets display the error message sent to us
         loginErrorMessage = error;
      }
   }

   void OnRoomList(Hashtable roomList) {

      // When room list is updated we are ready to move on to the island
      UnregisterSFSSceneCallbacks();
      Application.LoadLevel("TheGame");
   }



This is all mildly modified from the Island demo. You should really check that out, as they handle most of the SFS stuff in C#.

Server side...

Posted: 12 Sep 2010, 01:11
by CameronB
Here is what I have on the server side. This was straight from a tutorial, but I took the time to understand it, hopefully it will help you with your Java extension and understanding how that reacts with the client:

Code: Select all


   @Override
   public void handleInternalEvent(InternalEventObject ieo) {
      // TODO Auto-generated method stub
      String eventName = ieo.getEventName();
      trace("Event Fired: " + eventName);
      
      if (eventName.equals("loginRequest"))
      {
         boolean ok = false;
         User newUser = null;

         // Prepare a response object for the client
         ActionscriptObject response = new ActionscriptObject();

         // Get the user nickname and password
         String user = ieo.getParam("nick");
         String pass = ieo.getParam("pass");
         SocketChannel chan = (SocketChannel) ieo.getObject("chan");

         
         // validate the user name
         if (validNames.contains(user))
         {
            ok = true;
         }
         
         if (ok)
         {
            try
            {
               // Attempt to login the user to the system
               newUser = helper.canLogin(user, pass, chan, currentZone.getName());
            
               response.put("_cmd", "logOK");
               response.put("id", String.valueOf(newUser.getUserId()));
               response.put("name", newUser.getName());
               trace("Logged in user.");
               ok = true;
            }
            catch (LoginException le)
            {
               this.trace("Could not login user: " + user);
               response.put("_cmd", "logKO");
               response.put("err", le.getMessage());
            }
            
         }
         // The user name is not among the valid ones
         else
         {
            response.put("_cmd", "logKO");
            response.put("err", "Sorry, your user name was not accepted.");
            trace("Failed");
         }
         // Prepare the list of recipients, in this case we only one.
         LinkedList ll = new LinkedList();
         ll.add(chan);

         // Send login response
         sendResponse(response, -1, null, ll);   

         // Send room list
         if (ok)
            helper.sendRoomList(chan);
      }



The most important thing I think to look at is here:

response.put("_cmd", "logOK");
response.put("id", String.valueOf(newUser.getUserId()));
response.put("name", newUser.getName());
trace("Logged in user.");
ok = true;

You can see the server is stuffing that "logOK" message along with the ID, and the name of the logged in user, and sending it as an SFSObject to the client. The client then uses the code in my previous post to pull out those parameters and use them.

Hope this helps.

Posted: 14 Sep 2010, 19:52
by Robbilie
hey pls

Can anybody only translate the verry small javascript snippeet into c# for me?

Pls!

Thanks
Robert

Posted: 15 Sep 2010, 00:52
by Nanite
Robbilie wrote:hey pls

Can anybody only translate the verry small javascript snippeet into c# for me?

Pls!

Thanks
Robert


Hi,

Try to use this online converter http://www.m2h.nl/files/js_to_c.php

Posted: 15 Sep 2010, 04:17
by Robbilie
yes i had the same idea bit it doesnt work....

Parse error

Come on guys pls that isnt much code only a few lines..

Thanks
Robert

Posted: 15 Sep 2010, 07:49
by ThomasLund
The client side javascript/AS3 in the bottom is totally unusable for you as its very Flash client dependent.

You need to hook in your game code there with how you want to present things in _your_ gui. So no one can write that code for you except yourself.

Also - if you have trouble with this part, then good luck making an MMO. I would definitely start with a muuuuuuuch smaller game idea.

Good read in that regards:
http://sol.gfxile.net/mmorpg.html

Posted: 15 Sep 2010, 12:27
by Robbilie
thomad i dont think that i have to read that cause im verry sure that i will get my mmorpg to work..

Now the problem:
Tha extension works fine and it sends back in the debug logOk

But i dont now how to get this response to use in the game

And that was the sense of the snippet i think

Thats why i want somebody to translate it for me...

It would be very kind ;)

Thanks
Robert

Posted: 16 Sep 2010, 17:14
by CameronB
Robert,

I already basically translated it for you and showed you how I implemented the response catching in Unity. You need to take the time to read that and try it out and if you can't get that working, you might want to start with some java script tutorials.

If you learn Javascript as well as C#, you can translate things yourself and it will make your life a lot easier. Even if you can't 100% translate things, you will at least be able to get some idea of what's going on, so you can work it out yourself.

You also might be able to find a translator online.

Google Javascript to C# converter and do some research. There are a number of methods available.

-Cameron

Posted: 21 Sep 2010, 15:15
by appels
Thanks CameronB, your post helped me create my custom login :)

Posted: 21 Sep 2010, 20:00
by appels
i cheered to fast lol
my custom login is working but when i send a GetRoomList request, i don't get anything back.
is there anything else we need to add add in the extension for this ?
my OnRoomList function never gets called but i see i send the command in my logs.
Thanks.

Posted: 21 Sep 2010, 23:11
by appels
all good now, figured it out. still needed to login my user in the extension.

Posted: 29 Sep 2010, 13:45
by dr-mad
hi thx cameronb

but i got a smal bug just like appels say

i cant get in my room but what function do i use in the extention i try alot but i cant go in my world how do i fix this because i cant join my game room but i wil get connected

appels how did you fix your problem

and i see that cameronB just post a extention but i think it is a C# script can that be inplemented in a .as extention???


before i mess things up i just ask this

thanks if someone reply to my message

Posted: 30 Sep 2010, 03:40
by appels
I had 2 problems, i forgot to log the user into smartfox in the extension and on the client side I needed to call the roomlist. You need to call it yourself if you use a custom login.
My extension was pure as.