Server Extensions working with Unity
Posted: 12 Feb 2013, 16:56
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:
VoiceChat.java:
and here is the code for sending and receiving the packets:
I am thinking that I missed something important but can't seem to find it. Help would be much appreciated.
Thanks.
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");
}
}
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();
}
}
Thanks.