Skip to content

Instantly share code, notes, and snippets.

@erycson
Created February 18, 2016 14:00
Show Gist options
  • Save erycson/68136bb90687c78cc797 to your computer and use it in GitHub Desktop.
Save erycson/68136bb90687c78cc797 to your computer and use it in GitHub Desktop.
/*
* IndoorAtlas Unity3D Client
* Copyright (C) 2016 Érycson Nóbrega <egdn2004@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
namespace Assets.NewEyes.Scripts
{
public class Geolocation : MonoBehaviour
{
public static LocationInfo CurrentLocation;
private Thread thread;
void Start()
{
Debug.Log("Geolocation started");
thread = new Thread(StartRecive);
thread.Start();
}
/// <summary>
/// Connect to Unity Server
/// </summary>
/// <param name="obj"></param>
void StartRecive(object obj)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect("192.168.25.32", 7778);
Debug.Log("Connected to unity server");
byte[] buffer;
try
{
while (true)
{
buffer = new byte[1024];
client.Receive(buffer);
BinaryReader br = new BinaryReader(new MemoryStream(buffer));
CurrentLocation = new LocationInfo
{
Latitude = br.ReadDouble(),
Longitude = br.ReadDouble(),
Altitude = br.ReadDouble()
};
}
}
catch (Exception ex)
{
thread.Abort();
Debug.Log("Unity server disconnected");
}
}
void OnDestroy()
{
if (thread != null)
thread.Abort();
}
}
public class LocationInfo
{
public double Latitude;
public double Longitude;
public double Altitude;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment