• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by iamthecoolguy11 · Oct 21, 2014 at 09:04 PM · c#pluginjavasmartfoxsmartfoxserver

How can I connect unity to my smart fox server plugin

I followed a tutorial and had that working but I made a few changes to try and experiment to see how it works but I cant get it to work properly. Right now I tried to edit it so that it would send coordinates to the server and the server would send them back. But I get this error from the server. if you comment to help or try to give a answer I well make sure to thumb it up

alt text

if you cant read it open the picture URL to get a larger image http://piclair.com/data/8fopl.jpg

Here is the NetBeans project script

  package PlayerPosExtension;
 
 import com.smartfoxserver.v2.entities.User;
 import com.smartfoxserver.v2.entities.data.ISFSObject;
 import com.smartfoxserver.v2.entities.data.SFSObject;
 import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
 
 public class PlayerPos extends BaseClientRequestHandler{
 
     @Override
     public void handleClientRequest(User user, ISFSObject objIn) {
       
        int x = objIn.getInt("x");
        int y = objIn.getInt("y");
        int z = objIn.getInt("z");
        
        ISFSObject objOut = new SFSObject();
        objOut.putInt("PosX", x);
        objOut.putInt("PosY", y);
        objOut.putInt("PosZ", z);
        
        send("PosX", objOut, user);
        send("PosY", objOut, user);
        send("PosZ", objOut, user);
     }
     
 }
 

Here is the script in unity

 using UnityEngine;
 using System.Collections;
 using Sfs2X;
 using Sfs2X.Core;
 using Sfs2X.Requests;
 using Sfs2X.Entities;
 using Sfs2X.Entities.Data;
 
 public class SFS2X_Connect : MonoBehaviour 
 {
 public string ConfigFile = "Network/sfs-config.xml";
 public bool UseConfigFile = false;
 public string ServerIP = "127.0.0.1";
 public int ServerPort = 9933; 
 public string ZoneName ="BasicExamples"; 
 public string UserName = "hi";
 public string RoomName = "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, OnLogin);
         sfs.AddEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, OnConfigLoad);
         sfs.AddEventListener(SFSEvent.CONFIG_LOAD_FAILURE, OnConfigFail);
         sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
         sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
         sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
         sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse); 
         
         if(UseConfigFile)
         {
             sfs.LoadConfig(Application.dataPath + "/" + ConfigFile);
         }
         else
             {
                  sfs.Connect(ServerIP, ServerPort); 
             }
 
         sfs.Connect(ServerIP, ServerPort);
     }
 
     void OnConfigLoad(BaseEvent e)
     {
         Debug.Log("Config File Loaded");
         sfs.Connect(sfs.Config.Host, sfs.Config.Port);
     }
 
     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("JoinRoom Error (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
     }
 
     void OnConfigFail(BaseEvent e)
     {
         Debug.Log("Failed to load Config File");
     }
 
     void OnLogin(BaseEvent e)
     {
         Debug.Log("Loggged in: " + e.Params["user"]);
         sfs.Send(new JoinRoomRequest(RoomName));
 
         ISFSObject objOut = new SFSObject();
         objOut.PutInt("x", 2);
         objOut.PutInt("y", 5);
         objOut.PutInt("z", 1);
 
         sfs.Send(new ExtensionRequest("PosX", objOut));
         sfs.Send(new ExtensionRequest("PosY", objOut));
         sfs.Send(new ExtensionRequest("PosZ", objOut));
     }
     
     void OnExtensionResponse(BaseEvent e)
     {
         string cmd = (string)e.Params["cmd"];
         ISFSObject objIn = (SFSObject)e.Params["params"];
         
     
         Debug.Log("x :" + objIn.GetInt("PosX"));
         Debug.Log("y :" + objIn.GetInt("PosY"));
         Debug.Log("z :" + objIn.GetInt("PosZ"));
         
     }
     
     void OnLoginError(BaseEvent e)
     {
         Debug.Log("Loggged error: (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
     }
     
     void OnConnection(BaseEvent e)
     {
         if((bool)e.Params["success"])
         {
             Debug.Log("Successfully Connected");
             if(UseConfigFile)
             {        
                 ZoneName = sfs.Config.Zone; 
             }    
             sfs.Send(new LoginRequest(UserName, "",ZoneName));
         }
         else
         {
             Debug.Log("Connection Failed"); 
         }
     }
     
     void Update()
     {
         sfs.ProcessEvents(); 
     }
 
     
     void OnApllicationQuit()
     {
         if(sfs.IsConnected)
             sfs.Disconnect();
     }
 
     void OnGUI()
     {
         
     }
 }

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by eatsleepindie · Oct 21, 2014 at 09:50 PM

I've been working with SmartFox + Unity3D for over 3 years now, and I can tell you matter-of-factly that your problem is on the Server's end, not in Unity, although your Unity script is going to cause triple the needed bandwidth.

I don't mean to be blunt, but you're doing a lot of things wrong in these scripts. For example:

  ISFSObject objOut = new SFSObject();
  objOut.PutInt("x", 2);
  objOut.PutInt("y", 5);
  objOut.PutInt("z", 1);
  
  sfs.Send(new ExtensionRequest("PosX", objOut));
  sfs.Send(new ExtensionRequest("PosY", objOut));
  sfs.Send(new ExtensionRequest("PosZ", objOut));

You're packing your x,y,z coordinates into a single a SFSObject and sending it to the server three times... you only need to send it once since all three coordinates are included with each send.

Your SFS Server is failing when it boots because your extension is not implementing the basic SmartFox Server functionality at all, so it has no idea what to do with the data you are sending to it.

I'm not sure what sort of game you are working on and what you want to accomplish with these scripts, but I think you would be much better off reverting your extension to the original Basic Examples and working from there. If you're building an MMO, start with the MMO example... if you're building an FPS, they have an example for that as well.

Smartfox has a hell of a learning curve when it comes to custom extensions. They can be relatively easy to implement, but ridiculous when it comes to debugging due to the lack of good examples (and a community that is nowhere near as helpful as Unitys). And to top it off, a lot of the examples take short-cuts that make the system seem as though it's doing what they claim, when really the information is being fudged ( the Space-themed MMO project is a perfect example of this; they claim that it uses their MMOAPI, but when you break it down it's been fudged and things like Area-of-Interest aren't actually programmed to work... they just look like they work for the sake of the demo. That's not say that AOI in SmartFox MMO rooms doesn't work at all, it's just that the projects they offer as examples aren't going to a be a legit "how-to").

My best advice it read everything they have available on their site, and do so multiple times. Take lots of notes, but keep in mind that not all of their example code works (I've found at least 2 cases of example scripts in their tutorials section that don't work at all.. reported them both a long time ago, neither have been fixed).

I'm not trying to discourage you from SmartFox at all, I'm just saying that there are pretty much two cases here: 1. Go with the example extension that best suits your needs and build from there, or 2. if you're dead-set on building your own custom extension, put in the time to learn how smartfox works. Build simple, stupid extensions that do basic things and add on them.

Last but not least, as awesome as the Unity Community is, this isn't a place for SmartFox issues... SmartFox has their own forum for that, but I have only experienced crickets and accusations (from Mods themselves) there... everything I've done has been through absolute stubbornness and more man hours than I dare count. In fact, I assisted them once with an issue concerning their Windows 8 dlls; at first I was crucified by a Mod, then I proved I was right, and then the thread disappeared)

I'd be happy to offer you help along the way, but only after you've put in the time to learn the basics of how it works. Based off of your server extension code, you need to put a lot more time into reading the documentation and becoming more familiar with the API.

It's a crappy, lonely, pull-your-hair-out frustrating ride, but the rewards are pretty substantial :) If you give me an idea of what sort of project you are planning to make, I'd be more than happy to let you know which example project would be best to start out with... it may seem pretty obvious on the face of it, but you'd be surprised at how many corners they cut in their example scenes. Both the basic MMO and the SpaceMMO examples use the MMOAPI, but both cut substantial corners in different ways, so depending on what you hope to do with the game, one will probably be much better suited than the other.

Comment
iamthecoolguy11

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Oct 21, 2014 at 10:53 PM 0
Share

thanks man, I am doing a server for a massive multiplayer game or the mmo example but you seem to know a lot of this lol, and if you know any good documents if you want you can send me a link lol but thanks for the nice answer

avatar image eatsleepindie · Oct 22, 2014 at 02:18 AM 0
Share

First, make sure you're running the latest version of Smartfox (2.8 and earlier does not have the MMO API). Get this example working in both Unity and server-side:

http://docs2x.smartfoxserver.com/ExamplesUnity/mmo-demo

From there, you can do a ton of stuff with an MMO without ever needing to create a custom extension. The custom extension will come later, when you need to create custom classes server-side to track things like inventory, MMO Items, etc.

In the code you provided, you are trying to set a users position using a custom extension, when that is already completely done in the MMO API of smartfox. Docs for setting user positions and how the AOI works:

http://docs2x.smartfoxserver.com/AdvancedTopics/mmo-rooms http://docs2x.smartfoxserver.com/AdvancedTopics/advanced-mmo-api

Specifically for your case, the SetUserPosition request will do exactly what you are trying to do, but without the need for a custom extension. Get your basic client/server communications setup, employ any of the mmo api features you may need, and then worry about using a custom room extension to handle your custom data.

avatar image iamthecoolguy11 · Oct 25, 2014 at 03:14 AM 0
Share

thanks man I looked around on the site this one helps me the most so far witch you sent me http://docs2x.smartfoxserver.com/DevelopmentBasics/connection-phase just going threw all the tuts

avatar image eatsleepindie · Oct 25, 2014 at 05:37 AM 0
Share

Glad I could help and good luck with your project.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to write a java lib for unity as a plugin to make run another android app? 1 Answer

How to delete rooms in smartfox using C# 0 Answers

Distribute terrain in zones 3 Answers

Problem reading sensor plugin, event won't trigger 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges