- Home /
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
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()
{
}
}
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.
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
First, make sure you're running the latest version of Smartfox (2.8 and earlier does not have the $$anonymous$$$$anonymous$$O 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 $$anonymous$$$$anonymous$$O 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, $$anonymous$$$$anonymous$$O 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 $$anonymous$$$$anonymous$$O 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.
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
Your answer
Follow this Question
Related Questions
Problem reading sensor plugin, event won't trigger 2 Answers
java android plugin worker thread ends up in Unity mainthread 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to delete rooms in smartfox using C# 0 Answers