Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SmartFoxServer as backend through Unity?

Discussion in 'Multiplayer' started by Thomas-Lund, Mar 14, 2008.

  1. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Hey guys

    I have previously used SmartFox server for lobby system, and was wondering if anyone had written a .NET plugon for Unity to use it?

    It should be fully possible to do this, but you would have to code your own client API port (sources available).

    For anyone not seens SFS yet:

    http://www.smartfoxserver.com

    Really good pricing, good features, extensible but Flash only for now on client side.

    Searched both here, on SFS forums and through Google, but couldnt find much.

    So this is the last resort before trying to create a port myself.
     
  2. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    OK - reply to my own thread :-D

    Seems no one really has done this, so I talked a little with the SFS makers.

    I'm currently prototyping a SFS client API for Unity. Lets see if I make it through to the finish.

    Doesnt have top priority right now, but it doesnt seem to bad a job, and could expand multiplayer games with Unity a lot.

    Will keep you updated
     
  3. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Last update before going on a few days vacation.

    Got the client api to connect/disconnect, login and logout as well as return the list of rooms.

    All in a few hours of fiddling - so it is definitely feasable within a few days to port the majority of the client API to C#.

    I'll start with all the major things, and save away some of the more special stuff (like multiple protocols) for later/never. But I'll definitely do this now.
     
  4. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    It's totally feasible ... I have it up and running through our own API based on the Flash code that Smartfox gives you :)

    I really wish I could share some code, but it's all for 'the company' so it belongs to the company.

    All I can say is that it works REALLY well here. *Edited out some badly put crap I wrote here!*

    A lot of the server side nasty work is already done (dealing with buddy lists etc.) and you can't lose!

    I'm thinking about the possibility of using Unity's built-in networking AND smartfox ... the server could tell you IP addresses of connecting computers so that you can do all the matchmaking etc. with smartfox then go peer to peer for action-packed minigames and the like. It seems crazy, but it just might work ;)

    Good luck!
     
  5. David-Helgason

    David-Helgason

    Unity Technologies

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    Hey Jeff!

    That's not at all crazy, but very good sense :)

    d.
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Just remember that you need a facilitor for NAT punch through, but if you don't need to do it, that's just fine.

    I will have to research SmartFox myself. Sounds like a very useful tool.

    -Jeremy
     
  7. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Spend some transportation time going on a mini-vacation with the family to translate most parts of the API.

    Will have to revisit variou parts, but I think most of the "send from Unity to SFS" part works out of the box now.

    My main issue to crack will be how to handle the event callbacks. Any tips and tricks on how you guys did that?

    The SFS API for AS3 extends the Flash Event object. Now my lack of C# knowledge starts to show for good, as I have no clue about how to do it in C#. Google is my friend, but if you got a tip, then please spare me the hours spend searching :)

    Thanks!

    BTW - got contact to the SFS guys, and seems we can agree to the terms. So once I get this done it will be released for free as part of SFS - so everyone can use (and improve) it :)
     
  8. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    All right

    Easter egg surprise.

    I got the C# client API running now for real with 95% of functionality ported over :)

    Missing is support for HTTP protocol and JSON protocol (easy add ons) and sending generic objects to/from clients. The latter will take quite some coding to do, so that will be left for later.

    I'm still running some tests and fixing buggers, but basics work.


    But here comes the easter egg - it doesnt work in Unity. I have used Linq for handling the XML data, and that is a .NET 3.5 feature. I didnt realize before I was done that Unity runs .NET 2.0 only.

    So I will get things tested through now using a dedicated C# client, and then afterwards port the Linq code to "something .NET 2.0" for XML handling. BAH!


    If anyone has some good advice on XML handling that doesnt involve tons of code and is 2.0 compatible, then please contact me.
     
  9. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Perhaps if you ask real nice you'd get a hint at when the latest edition of mono will be added to unity. In all probability you won't get a date, but if you're lucky you might get a "this spring / summer / fall / winter".
     
  10. kellberg

    kellberg

    Joined:
    Jan 13, 2008
    Posts:
    6
    You should be able to use the mono dll's for LINQ - try to include them in your project and seen if unity will compile it.
     
  11. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    OK - bit the bullet and converted everything down to .NET 2.0

    Now compiles, and I'll try to get it running in Unity today :)

    Test at 3 am this morning didnt go well. The C# client works perfectly, but Unity as client doesnt.

    It is as if it doesnt receive the response from the SFS server or doesnt call the delegate callback.
    I wonder if there is some namespace issue or something here when the dll runs in Unity.

    Example (reworked for here) client code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Runtime.InteropServices;
    6. using System.Security.Permissions;
    7. using System.Text;
    8. using SmartFoxClientAPI;
    9.  
    10. public class SmartFoxClientTest : MonoBehaviour {
    11.     public SmartFoxClient smartFox;
    12.  
    13.     void Start ()
    14.     {
    15.         smartFox = new SmartFoxClient(true);
    16.  
    17.         // Register callback delegate
    18.         SFSEvent.onConnectionCallback += OnConnection;
    19.  
    20.         // "Multicast" of event
    21.         SFSEvent.onLoginCallback += OnLogin;
    22.         SFSEvent.onLoginCallback += OnLogin2;
    23.  
    24.         Debug.Log("Test: " + smartFox.getVersion());
    25.  
    26.         smartFox.connect("127.0.0.1", 9342);
    27.     }
    28.    
    29.     public void OnConnection(Boolean success, String error)
    30.     {
    31.         Debug.Log("On Connection callback got: " + success + "\n\r" + error);
    32.         smartFox.login("xflow", "thomas", "");
    33.     }
    34.  
    35.     public void OnLogin(Boolean success, String name, String error)
    36.     {
    37.         Debug.Log("On Login callback got: " + success + "\n\r" + error + "\n\r" + name);
    38.     }
    39.  
    40.     public void OnLogin2(Boolean success, String name, String error)
    41.     {
    42.         Debug.Log("On Login2 callback got: " + success + "\n\r" + error);
    43.     }
    44.    
    45.     void OnApplicationQuit (){
    46.         try{
    47.             smartFox.disconnect();
    48.         }
    49.         catch{}
    50.     }
    51. }
    52.  
    None of the callbacks ever get called - but I can see on the server that the connect and login work.
     
  12. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Chapter 2 of this test round.

    It seems that its the networking part that is the problem.

    I did a small delegate callback test that is not triggered from network response but from a method call, and that worked.

    So the issue is that somehow the socket that is opened to the SFS server from the Unity based client doesnt read the response send back!

    Has anyone tried this before from inside unity?

    I'm doing this:


    Code (csharp):
    1.  
    2. StreamWriter writer = new StreamWriter(socketConnection.GetStream());
    3. writer.Write(msg + (char)0);
    4. writer.Flush();
    5.  
    to send the message to the SFS server. Works fine!

    The socketConnection is defined as follows and uses async callback for reading the response.

    Code (csharp):
    1.  
    2. socketConnection = new TcpClient();
    3. socketConnection.Connect(ipAdr, port);
    4. socketConnection.GetStream().BeginRead(byteBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(handleSocketData), null);
    5.  
    Works fine in a standalone C# client, but apparently not inside Unity.

    I should maybe post this as a separate support issue.
     
  13. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    BAH!!!

    Good news and bad news.

    Good: Got it running now in Unity too!!
    Bad: My ego has taken a few bruises

    Hehe

    Dont know why it worked in the C# client - shouldnt have really - but it does work in Unity now.

    Was neither an issue with delegates or network replies. Was all a matter of a null reference exception that didnt get caught.
     
  14. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    We've been toying around about using SFS for a backend and I'm very interested in your port of the client. Don't know if I can help out, but I'm offering.
     
  15. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Thanks for the offer!

    At the moment all thats left is (more or less) integrating a serializer that Marco from SFS wrote for me, testing and fixing comments to be C# style.

    If you want to run some testing, then I would be super happy! I've done my own, but only tested about 50% of the commands yet.

    Buzz me if you want a copy of the code and have time to test drive it.

    Its going to be part of the official SFS release, so (soon?) its public
     
  16. Lapo

    Lapo

    Joined:
    Apr 11, 2008
    Posts:
    7
    Interesting :)
    Could you give me more details about why real-time stuff would not be fast enough? We have many clients working with Flash real time games (racing games, shooters) and they achieved excellent results.
    I'd like to learn more about possible Unity/SFS integrations and see if we can introduce enhancements on this front

    Thanks in advance

    Lapo
    p.s. = btw, I am the lead the developer of SmartFoxServer
     
  17. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Hey!

    Reading back on my post, that wasn't quite what I meant and I put it pretty badly. Must have been a late night or something ;)

    I meant that the idea of, for example, producing Unreal Tournament or Halo-style multiplayer using SmartFox isn't a realistic one.

    Sorry if I caused confusion. You're totally right, I've seen (and put together) realtime multiplayer with SmartFox using both Flash AND Unity3d and it's awesome!

    :)
     
  18. Lapo

    Lapo

    Joined:
    Apr 11, 2008
    Posts:
    7
    Ok great, thanks :)

    Yep, I see... and I agree. At least until we add UDP support, then why not? :wink:
    SmartFoxServer is essentially targeted at the Flash Player, which still doesn't provide any UDP suport. It seem it should come with version 10, expected this year.

    Cool :) I'd be really interested in seeing what you have done. Is there anything publicly available?

    Thanks

    Lapo
     
  19. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    If I read the website correctly you would need the pro version and bluebox, which is around 2400 euros. That would allow for authentication, buddy lists and NAT punch through for unlimited users. Seems a bit expensive for indies to me, if you're just going to use it as an authenticated master server/lobby setup.

    Take the generic master server, add a little SQL here and a little SHA256 there, stir well and cook at FFFF degrees. Out pops an authenticated master/lobby for a lot less money that you can throw on a cheap unix box.

    I'd be inclined to look at this product again if they had a separate (and far cheaper) license that allowed for just an authenticated master/lobby and not for use of the networking layer for games themselves.
     
  20. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Hey Lapo,

    Nothing publicly available ... yet ... soon, though (if all goes well!) :)

    UDP support in SmartFox? That would be awesome .. seriously, that'd be something we would be really interested in looking at.

    Thanks - sorry again for my badly written original message!

    Jeff.
     
  21. hungkv

    hungkv

    Joined:
    Apr 14, 2008
    Posts:
    12
    Hi Thomas!

    I have Socket Server, i want to develop a client by Unity C# script. But my client can't receive message form server.
    Can you help me?


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Text;
    5. using System.IO;
    6. using System.Net;
    7. using System.Net.Sockets;
    8.  
    9. public class Mytest : MonoBehaviour {
    10.  
    11.     // Use this for initialization
    12.     Socket sock;
    13.     int counter = 0;   
    14.     void Start()
    15.     {
    16.         IPAddress host = IPAddress.Parse("10.4.2.215");
    17.         IPEndPoint hostip = new IPEndPoint(host, 3333);
    18.         sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    
    19.         try
    20.         {
    21.             sock.Connect(hostip);
    22.             print("Connected successful");
    23.         }
    24.         catch (SocketException e)
    25.         {
    26.             print("Problem connecting to host");
    27.             print(e.ToString());
    28.             return;
    29.         }
    30.     }
    31.        
    32.     // Update is called once per frame
    33.     void Update ()
    34.     {  
    35.         if(sock.Connected == true)
    36.         {
    37.            
    38.             byte[] receivedbytes = new byte[512];
    39.            
    40.             int read = sock.Receive(receivedbytes);
    41.             string message=Encoding.ASCII.GetString(receivedbytes, 0, read);
    42.             print(message);
    43.         }
    44.     }  
    45.          
    46. }
    47.  
    48.  
    Thanks!
     
  22. scottobot21

    scottobot21

    Joined:
    Jun 13, 2008
    Posts:
    5
    *bump*

    Hey all! Any news on the Unity+SFS effort? This is something I would love to contribute to, if possible.
     
  23. Lapo

    Lapo

    Joined:
    Apr 11, 2008
    Posts:
    7
    Hi,
    yup the project is nearly completed. We're currently working on the tests which usually take more than development itself :p
    Anyways, we should be able to release a beta by the middle of August (Thomas Lund what do you say?).
    If you wish to get it, please simply drop us an email as a reminder, we'll contact you back as soon as we're ready.

    Mail -> info :at: smartfoxserver.com

    Thank you
     
  24. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Hehe, Thomas will be back today from vacation to answer. :)
     
  25. waldo

    waldo

    Joined:
    Jul 25, 2008
    Posts:
    21
    Nice!
     
  26. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Definitely think that open beta is just around the corner!
     
  27. TheLorax

    TheLorax

    Joined:
    Mar 25, 2007
    Posts:
    86
    beta bump... any news folks?


    thanks


    the lorax
     
  28. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Just days away :)
     
  29. waldo

    waldo

    Joined:
    Jul 25, 2008
    Posts:
    21
  30. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Ahhhh - you beat me to it :)

    Yes!!! Its finally out - public beta 1 of the SFS .net client. Including a fantastic beautiful (=ugly as only a non-GUI coder can make it) Unity tech demo showing off a rudimentary lobby system with chat.

    Please give it a spin everyone, and report back all issues you find!

    There is a forum for reporting back on issues found. Fire away here:

    http://www.smartfoxserver.com/forums/viewforum.php?f=11

    I'll soon try to make a "real" 3d demo showing how to make a simple virtual chatroom = more game related than the lobby part.

    SFS can both be used for lobby and for providing game server functionality. Its a real cool system, and I'll try to provide you guys with some tutorials and stuff soon. But have to find time for game development too :-D
     
  31. ianreynolds77

    ianreynolds77

    Joined:
    May 8, 2008
    Posts:
    3
    Think this is great work you guys have been doing.

    I tested the client server last night and it is working fine. Simple demo but proves the concept works.

    I was wondering if you ever got round to creating a simple virtual world with smartfox integration?

    I am working on something like this myself at the moment. Will keep you posted on my findings but any advice you could give would be great. What do you think about integrating streaming audio and video to Unity via the Redbox add-on.

    Ian :D
     
  32. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Hi Ian

    I know of several projects that use the SFS client for a virtual world simulation right now. So its definitely usable.

    Also - beta 2 is just around the corner with some connection fixes and BlueBox support.

    I havent looked at RedBox support yet at all - not even to the point if there is even anything to code or not in the API. I will take a peek soonish after beta 2!

    Keep me posted on your progress!
     
  33. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Hi, first off thanks for doing this and for mentioning it in my previous MMO related post! I''d love to see a more advanced project using this; if only so myself and the rest of the community can see how it's done to aid in our learning :).

    Adam.
     
  34. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    With some help from Thomas's example in C#, I am in the process of writing a custom Java based extension which is being used to test data throughput for a multiplayer game (avoiding the term MMO at the moment, don't have the machine power nor the bandwidth for such a test)

    The main test though is the prediction queue. The ai extension module and the battle extension module are rather interesting and diverse. This server model is also very interesting in that destination information is directed to specific zones / rooms that they call it and not a complete broadcast. So the extensions that you write, you have to tell the zone / room that you want that zone to use that extension (or rather have it available to be used).

    Which reminds me, need to get the next beta build from you Thomas :)
    -Zumwalt
     
  35. yazzgoth

    yazzgoth

    Joined:
    Oct 7, 2008
    Posts:
    7
    I would really love to see SFS support built in Unity3D - especially for it to work with the web plugin.
     
  36. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Hi Yazzgoth

    The plugin works in the webbrowser too. Straight up with no differences to a standalone build.

    So take the plunge and give it a spin!
     
  37. yazzgoth

    yazzgoth

    Joined:
    Oct 7, 2008
    Posts:
    7
    I assumed it's not working as it just crashes the browser after I press connect.
    Any idea why it may crash like this?
     
  38. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Current release is a beta 1. It has a few issues, but I got it running in a browser and know of several others who have it running too in a browser build.

    Its not very tolerant to errors (improved a lot in beta 2), and I guess that there are some issues in the code that you got.

    You are most welcome to PM me your code and I can take a look, or post it on the SFS support forums.

    Also try to look at the Unity test project that ships with the dll and work from that.

    I have the unreleased beta 2 sitting on my harddrive - just waiting to be released. I can send you that dll if you want also.
     
  39. yazzgoth

    yazzgoth

    Joined:
    Oct 7, 2008
    Posts:
    7
    Please do send me these files.

    As for the crash - that happens with the project that comes with SFS .NET API I haven't started integrating it to my own project yet.
     
  40. CiroContns

    CiroContns

    Unity Legend

    Joined:
    Jan 28, 2008
    Posts:
    81
    Hi there,
    I was just guessing:
    can you let Unity-developed iPhone application and Flash made browser games interact with each other?

    For example, we have a game made for the web in Flash (UFHO) that we are porting to the iPhone. We use SFS for net play, can we make the iPhone gamers play with the Flash ones?

    Thanks to anyone who answers!

    Ciro Continisio
     
  41. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    Yes - you can do exactly that once the iPhone SFS API port has been done.
     
  42. ronin

    ronin

    Joined:
    Nov 22, 2008
    Posts:
    3
    Isn't Http Tunnelling a main feature of Bluebox?
    According to this:

    "they[Unity3D APIs] provide full support to the SmartFoxServer features with the exclusion of the HTTP tunnelling feature."

    http://www.smartfoxserver.com/labs/API/

    So what I'm asking is whether or not Bluebox is supported in the Unity3D API provided by SmartFoxServer.
     
  43. Thomas-Lund

    Thomas-Lund

    Joined:
    Jan 18, 2008
    Posts:
    465
    It is a feature of the beta 2, but there is still a lingering issue with it.

    Just like beta 1 had a problem with Unity/mono not liking async socket communication (crash on shutdown), beta 2 ends up having the same issue once that communication fails over to http tunnelling. It is also doing async instead of pure threaded.

    So recap - beta 2 fixed the socket shutdown crash. It also supports http failover, but your player will crash when being shut down if http was used. Will be fixed in the final (hopefully) beta 3.

    Its not a problem in the API code, but a problem in the Mono library used by Unity afaik. So got to rewrite perfectly working code to work around that issue - grrrrrrrr.
     
  44. ronin

    ronin

    Joined:
    Nov 22, 2008
    Posts:
    3
    Understood. Thanks.
     
  45. ronin

    ronin

    Joined:
    Nov 22, 2008
    Posts:
    3
    Hey Thomas,
    Thanks for the early x-Mas gift. :D