Skip to content

Instantly share code, notes, and snippets.

@William-ST
Created July 19, 2017 13:19
Show Gist options
  • Save William-ST/3b31c9adc3b664d59e6b51823cd34281 to your computer and use it in GitHub Desktop.
Save William-ST/3b31c9adc3b664d59e6b51823cd34281 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
public class Client : MonoBehaviour {
public int changePeoples = 0;
public String ACTION = "";
public String ACTION_PEOPLE = "aciton_people";
public String ACTION_EVENT = "action_event";
GameObject[] peoplesScene;
GameObject[] tempAudioScene;
AudioSource[] audioScene;
// Use this for initialization
void Start () {
SetupServer();
peoplesScene = GameObject.FindGameObjectsWithTag("people");
tempAudioScene = GameObject.FindGameObjectsWithTag("sound");
Debug.Log("tempAudioScene: " + tempAudioScene.Length);
Debug.Log("peoplesScene: " + peoplesScene.Length);
for (int i = 0; i < peoplesScene.Length; i++)
{
peoplesScene[i].SetActive(false);//GetComponent<Renderer>().enabled = false;
}
audioScene = new AudioSource[tempAudioScene.Length];
for (int j = 0; j < tempAudioScene.Length; j++)
{
audioScene[j] = tempAudioScene[j].GetComponent<AudioSource>();
Debug.Log("audio " + j + " volumen: " + ((1 - changePeoples) / peoplesScene.Length));
audioScene[j].volume = 0;
}
}
// Update is called once per frame
void Update () {
Debug.Log("update: "+ACTION);
if (ACTION.Equals(ACTION_PEOPLE))
{
ACTION = "";
Debug.Log("execute: "+ ACTION_PEOPLE);
int currentPeople = 0;
for (int i = 0; i < peoplesScene.Length; i++)
{
if (currentPeople <= changePeoples)
{
peoplesScene[i].SetActive(true);
currentPeople++;
}
else
{
peoplesScene[i].SetActive(false);
}
}
for (int j = 0; j < tempAudioScene.Length; j++)
{
audioScene[j] = tempAudioScene[j].GetComponent<AudioSource>();
Debug.Log("audio "+j+" volumen: "+((1 - changePeoples) / peoplesScene.Length));
audioScene[j].volume = (1 - changePeoples) / peoplesScene.Length;
}
}
else if (ACTION.Equals(ACTION_EVENT))
{
ACTION = "";
}
else {
Debug.Log("update else");
}
}
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];
private void SetupServer()
{
Debug.Log("SetupServer");
try
{
_clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, 3004));
Debug.Log("connect!!");
}
catch (SocketException ex)
{
Debug.Log(ex.Message);
}
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private void ReceiveCallback(IAsyncResult AR)
{
//Check how much bytes are recieved and call EndRecieve to finalize handshake
int recieved = _clientSocket.EndReceive(AR);
if (recieved <= 0)
return;
//Debug.Log("recieved: "+ recieved);
//Debug.Log("_recieveBuffer: " + _recieveBuffer.Length);
//Debug.Log("_recieveBuffer: "+ _recieveBuffer.ToString());
//Copy the recieved data into new buffer , to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer, 4, recData, 0, recieved);
//Process data here the way you want , all your bytes will be stored in recData
//Debug.Log("---- --- : " + Encoding.ASCII.GetString(_recieveBuffer) + " : ");
//Debug.Log("---- --- : " + Encoding.ASCII.GetString(recData) + " : ");
String message = Encoding.ASCII.GetString(recData);
Debug.Log("message: "+message);
if (message.IndexOf("E(") != -1 && message.IndexOf(")") != -1)
{
Debug.Log("ARRIVED E!");
//hay un evento
}
else if (message.IndexOf("P(") != -1 && message.IndexOf(")") != -1)
{
Debug.Log("ARRIVED P!");
//hay un cambio de personas
// change scene
String nums = message.Substring(message.IndexOf("(") + 1, message.IndexOf(")")-message.IndexOf("(")-1);
Debug.Log("nums: " + nums);
changePeoples = int.Parse(nums);
ACTION = ACTION_PEOPLE;
Debug.Log("set: " + ACTION_PEOPLE);
}
else
{
Debug.Log("ARRIVED NN!");
}
//byte[] bufer = new byte[1024];
//char[] chars = new char[recieved];
//System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
//int charLen = d.GetChars(bufer, 0, recieved, chars, 0);
//System.String recv = new System.String(chars);
//Debug.Log("--- --- :" + recv);
//Start receiving again
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private void SendData(byte[] data)
{
SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
socketAsyncData.SetBuffer(data, 0, data.Length);
_clientSocket.SendAsync(socketAsyncData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment