Create a gist now

Instantly share code, notes, and snippets.

@kou-yeung /Client.cs Secret
Last active Feb 17, 2017

// Clientは Unityを使います
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System;
public class Application : MonoBehaviour
{
TcpClient client;
NetworkStream stream;
void Start ()
{
client = new TcpClient("127.0.0.1", 2001);
stream = client.GetStream();
}
public void Poll()
{
while (client.Available > 0)
{
Command cmd;
if(TryParseCommand(out cmd))
{
commandExec(this, cmd);
}
}
}
bool TryParseCommand(out Command cmd)
{
// stream -> Command : 今回は解説しない
return true;
}
void Update ()
{
Poll();
}
// ボタンクリックイベント
public void OnClick()
{
var bytes = Pack("Hello World!!!!");
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
// Workaround : Jsonなどの形式にシリアライズ。(各自実装)
byte[] Pack(object obj)
{
// object -> byte[]
return new byte[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment