DSIK_project/ShooterDSIK_newbac/Assets/Scripts/VideoDownloader.cs
Konrad Pierzyński e1234ad7c0 Added Unity Client
2018-11-29 17:20:24 +01:00

77 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Linq;
using UnityEngine;
using UnityEngine.Video;
using System;
using UnityEngine.UI;
using System.Threading;
public class VideoDownloader : MonoBehaviour
{
public VideoPlayer player;
public Canvas canvas;
// Use this for initialization
void Start()
{
player.Stop();
}
void StartDownloadingBG()
{
Socket sock;
IPAddress ip;
EndPoint serverEP;
FileStream fs;
System.Random rnd = new System.Random();
int file = rnd.Next(10000);
byte[] buffer = new byte[1024];
byte[] sizeBuffer = new byte[8];
fs = File.Open(System.IO.Path.GetTempPath() + "/" + file.ToString() + ".mp4", FileMode.OpenOrCreate, FileAccess.ReadWrite);
ip = canvas.GetComponent<GetServerIp>().GetIP();
serverEP = new IPEndPoint(ip, 8989);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.ReceiveTimeout = 1000;
sock.Connect(serverEP);
print(sock.Receive(sizeBuffer));
int bytes = 0;
long size = BitConverter.ToInt64(sizeBuffer, 0);
print(size);
while ((size -= bytes) > 0)
{
bytes = sock.Receive(buffer);
fs.Write(buffer, 0, bytes);
}
fs.Close();
sock.Close();
player.url = System.IO.Path.GetTempPath() + "/" + file.ToString() + ".mp4";
player.Play();
}
// Update is called once per frame
void Update()
{
}
}