Server Extensions working with Unity

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Post Reply
mebertrand
Posts: 2
Joined: 12 Feb 2013, 16:13

Server Extensions working with Unity

Post by mebertrand »

Hi,

I am working on a project in Unity 4 that requires voice and I am trying to send the voice packets through smartfox using the extensions and then play the voice on the clients. according to the server it is receiving the packets and sending packets but it doesn't seem to be using the extension I set up, or if it is then it is not sending the packets out in the right way.

The extension is a very basic one:

MainExtension.java:

Code: Select all

package com.voiceChat.sfs2x;

import com.smartfoxserver.v2.extensions.SFSExtension;

public class MainExtension extends SFSExtension {

	@Override
	public void init() {
		this.addRequestHandler("sendVoice", VoiceChat.class);
		trace("Package received");

	}

}
VoiceChat.java:

Code: Select all

package com.voiceChat.sfs2x;

import java.util.List;

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 VoiceChat extends BaseClientRequestHandler {

	@Override
	public void handleClientRequest(User player, ISFSObject params) {

		trace("I am handling data");
		MainExtension parent = (MainExtension)getParentExtension();
		
		Room room = parent.getParentRoom();
		List<User> recipients = room.getUserList();
		ISFSObject rtn = new SFSObject();
		rtn = params;

		parent.send("voice", rtn, recipients, true);
	}

}

and here is the code for sending and receiving the packets:

Code: Select all

using System;
using System.Collections;
using UnityEngine;

using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Entities.Data;
using Sfs2X.Requests;
using Sfs2X.Logging;
using Sfs2X.Util;

public class VoiceChatUnityClient : MonoBehaviour
{
    VoiceChatNetworkProxy proxy;

    private SmartFox smartFox; // the reference to SFS client

    void Start()
    {
        smartFox = SmartFoxConnection.Connection;
        VoiceChatRecorder.Instance.NetworkId = smartFox.MySelf.Id;
        Debug.Log(VoiceChatRecorder.Instance.NetworkId);
        if (smartFox == null)
        {
            Application.LoadLevel("Login");
            return;
        }

        SubscribeDelegates();
        proxy = VoiceChatUtils.CreateProxy();
    }

    private void SubscribeDelegates()
    {
        smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
        //smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
    }

    private void UnsubscribeDelegates()
    {
        OnDisconnectedFromServer();
        smartFox.RemoveAllEventListeners();
    }

    void OnDisconnectedFromServer()
    {
        GameObject.Destroy(proxy.gameObject);
    }

    private void OnConnectionLost(BaseEvent evt)
    {
        UnsubscribeDelegates();
        Application.LoadLevel("Login");
    }

    public void SendVoice(VoiceChatPacket packet)
    {
        Room room = smartFox.LastJoinedRoom;
        Debug.Log(room.ToString());
        ISFSObject data = new SFSObject();
        short compression = (short)(byte)packet.Compression;
        ByteArray byteArray = new ByteArray(packet.Data);
        data.PutShort("compression", compression);
        data.PutByteArray("data", byteArray);
        data.PutInt("length", packet.Length);
        data.PutInt("networkID", packet.NetworkId);


        Debug.Log("data put together");

        ExtensionRequest request = new ExtensionRequest("sendVoice", data, room, true); // True flag = UDP
        smartFox.Send(request);

        Debug.Log("Data sent");

        //HandleVoicePacket(data);
    }

    private void OnExtensionResponse(BaseEvent evt)
    {
        Debug.Log("received extension request with cmd of " + (string)evt.Params["cmd"]);
        try
        {
            string cmd = (string)evt.Params["cmd"];
            ISFSObject dt = (SFSObject)evt.Params["params"];

            if (cmd == "voice")
            {
                HandleVoicePacket(dt);
            }
        }
        catch (Exception e)
        {
            Debug.Log("Exception handling response: " + e.Message + " >>> " + e.StackTrace);
        }
    }

    private void HandleVoicePacket(ISFSObject dt)
    {
        Debug.Log("Received voice packet");
        short compression = dt.GetShort("compression");
        int userID = dt.GetInt("networkID");
        int length = dt.GetInt("length");
        ByteArray data = dt.GetByteArray("data");

        VoiceChatPacket packet = new VoiceChatPacket();
        packet.Compression = (VoiceChatCompression)(byte)compression;
        packet.Data = data.Bytes;
        packet.Length = length;
        packet.NetworkId = userID;
        //packet.Compression = new VoiceChatCompression();
        VoiceChatPlayer player = GameObject.Find("VoiceChat_Recording").GetComponent<VoiceChatPlayer>();
        player.OnNewSample(packet);
    }

    void OnApplicationQuit()
    {
        UnsubscribeDelegates();
    }
}
I am thinking that I missed something important but can't seem to find it. Help would be much appreciated.

Thanks.
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Server Extensions working with Unity

Post by rjgtav »

Hello,

First of all, you posted this question on the Server Side Extension Development section of SFS Pro/Basic, but you're using SFS2X, which is a different product. I'll move it to the SFS2X Questions, which is where all the SFS2X server-side related questions go. Don't worry, it's a common mistake ;)

Regarding your question, you're sending the extension request to the Room-Level, as you're specifying a room object. Is your extension at the Room-Level?
If it is on the Zone-level, then you can simply specify null as the room parameter.

Cheers
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
mebertrand
Posts: 2
Joined: 12 Feb 2013, 16:13

Re: Server Extensions working with Unity

Post by mebertrand »

yes, the extension is at room level, the zone level has some different extensions being used. I did manage to find what was wrong (resulting in a big face-palm), I forgot to call smartFox.ProcessEvents().
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Server Extensions working with Unity

Post by rjgtav »

Oh, yes, on unity you have to be always calling that method.. Glad you found the problem :)
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Post Reply