I have just started to learn SmartFoxServer 2x and i'm currently trying to create a working server side extension. It is a very simple one, all it does is to create a request handler to deal with messages sent from unity and then send it back. Obviously, i can't make it work. I manage to connect to the server, login and join a zone and a room. I join a zone called test and the room "The Lobby". In "The Lobby" room i am using testExtension as my main class. When i try to send a message to the server the runtime statistics confirm that it is indeed receiving some incoming traffic. But after that, nothing happens. I've tried remote debugging my extension but i've ran into a lot of problems there as well and posted a topic here at the forums about it, but so far no success. I really hope you can help me as i am very eager to start working with SFS2X!
I know it's a pain looking through all this code, but i'll try to make it as easy as possible and not only include all the code, but also highlights of the most important parts.
The server side extensions
testExtension.java
Code: Select all
package my.test;
import com.smartfoxserver.v2.extensions.SFSExtension;
public class testExtension extends SFSExtension {
public void init(){
addRequestHandler("test", testHandler.class);
}
}
testHandler.java
Code: Select all
package my.test;
import com.smartfoxserver.v2.entities.Room;
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 testHandler extends BaseClientRequestHandler{
@Override
public void handleClientRequest(User user, ISFSObject sfsObject) {
int number = sfsObject.getInt("test");
ISFSObject data = new SFSObject();
data.putInt("number", number * 2);
this.send("test", data, this.getParentExtension().getParentRoom().getPlayersList(),false);
}
}
Client (Unity)
Code: Select all
using UnityEngine;
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 SmartFoxTesting : MonoBehaviour {
private SmartFox smartFox;
private string ipAdress;
private float btnX, btnY, btnW, btnH, txtFldH;
bool loggedIn;
void FixedUpdate() {
smartFox.ProcessEvents();
}
// Use this for initialization
void Awake () {
loggedIn = false;
Application.runInBackground = true;
smartFox = new SmartFox(false);
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
smartFox.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnCreateRoomError);
smartFox.AddEventListener(SFSEvent.ROOM_ADD, OnRoomAdd);
smartFox.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
btnX = Screen.width * 0.05f;
btnY = Screen.width * 0.05f;
btnW = Screen.width * 0.075f;
btnH = Screen.width * 0.025f;
txtFldH = 20;
ipAdress = "192.168.1.3";
}
// Update is called once per frame
void Update () {
}
public void OnExtensionResponse(BaseEvent evt){
if(evt.Params["cmd"] == "test"){
Debug.Log("Succes!" + evt.Params["number"]);
}
Debug.Log("Nice");
}
public void OnRoomJoinError(BaseEvent evt){
Debug.Log("Error joining room " + evt.Params["error"]);
}
public void OnConnection(BaseEvent evt){
Debug.Log("Connected to " + ipAdress);
}
public void OnConnectionLost(BaseEvent evt){
Debug.Log("Disconnected");
loggedIn = false;
}
public void OnLogin(BaseEvent evt){
Debug.Log("Logged into " + evt.Params["zone"]);
loggedIn = true;
smartFox.Send(new JoinRoomRequest("The Lobby"));
}
public void OnJoinRoom(BaseEvent evt){
Debug.Log("Joined " + evt.Params["room"]);
}
public void OnCreateRoomError(BaseEvent evt){
Debug.Log("Error creating room: " + (string)evt.Params["error"]);
}
public void OnRoomAdd(BaseEvent evt){
string error = (string)evt.Params["error"];
Debug.Log("Added room: " + error);
}
void OnGUI(){
if(!smartFox.IsConnected){
ipAdress = GUI.TextField(new Rect(btnX, btnY, btnW, txtFldH), ipAdress);
if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"Connect")){
smartFox.Connect(ipAdress, 9933);
}
if(GUI.Button(new Rect(btnX, btnY * 1.2f + 2 * btnH, btnW, btnH),"Disconnect")){
smartFox.Disconnect();
}
}
else if(!loggedIn){
if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"Login")){
smartFox.Send ( new LoginRequest("Birch", "", "test"));
}
if(GUI.Button(new Rect(btnX, btnY * 1.2f + 2 * btnH, btnW, btnH),"Disconnect")){
smartFox.Disconnect();
}
}
else if(!(smartFox.LastJoinedRoom == smartFox.GetRoomByName("The Lobby"))){
if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH),"New Game")){
RoomSettings roomSettings = new RoomSettings("game");
roomSettings.GroupId = "default";
roomSettings.MaxUsers = 10;
roomSettings.IsGame = true;
roomSettings.MaxSpectators = 0;
smartFox.Send(new CreateRoomRequest(roomSettings,true,smartFox.LastJoinedRoom));
}
}
else{
if(GUI.Button(new Rect(btnX, btnY * 1.2f + btnH, btnW, btnH), "Send Message")){
ISFSObject data = new SFSObject();
data.PutInt("test", 1);
ExtensionRequest request = new ExtensionRequest("test",data,smartFox.LastJoinedRoom, false);
smartFox.Send(request);
}
}
}
}
Important parts:
Sending the message
Code: Select all
ISFSObject data = new SFSObject();
data.PutInt("test", 1);
ExtensionRequest request = new ExtensionRequest("test",data,smartFox.LastJoinedRoom, false);
smartFox.Send(request);
Receiving the message
Code: Select all
public class testExtension extends SFSExtension {
public void init(){
addRequestHandler("test", testHandler.class);
}
}
Dealing with and returning the message
Code: Select all
public class testHandler extends BaseClientRequestHandler{
@Override
public void handleClientRequest(User user, ISFSObject sfsObject) {
int number = sfsObject.getInt("test");
ISFSObject data = new SFSObject();
data.putInt("number", number * 2);
this.send("test", data, this.getParentExtension().getParentRoom().getPlayersList(),false);
}
}
Receiving the message in Unity
Code: Select all
smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
.
.
.
public void OnExtensionResponse(BaseEvent evt){
if(evt.Params["cmd"] == "test"){
Debug.Log("Succes!" + evt.Params["number"]);
}
Debug.Log("Hmmm...");
}