Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Last active February 17, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/d9cada3653ed1b6acccdfc7a08e8564c to your computer and use it in GitHub Desktop.
Save kou-yeung/d9cada3653ed1b6acccdfc7a08e8564c to your computer and use it in GitHub Desktop.
// 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