SHARE
TWEET

Untitled

a guest Jul 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Net;
  9. using System.Threading.Tasks;
  10.  
  11. public class ChatClient : MonoBehaviour {
  12.  
  13.     // ゲームオブジェクト
  14.     public InputField input_field;
  15.     public GameObject content;
  16.     public GameObject chat_log;
  17.  
  18.     // サーバー情報
  19.     private string server_ip_address = "192.168.0.25";
  20.     private int port = 25252;
  21.  
  22.     // クライアントの送信窓口
  23.     NetworkStream network_stream;
  24.  
  25.     // 送信内容
  26.     private string send_text;
  27.  
  28.     // 受信用
  29.     private TcpListener listener;
  30.     private float request_span = 10f;
  31.     private byte[] buffer = new byte[4096];
  32.  
  33.  
  34.     // Start is called before the first frame update
  35.     void Start() {
  36.         //Componentを扱えるようにする
  37.         input_field = input_field.GetComponent<InputField>();
  38.  
  39.         // 監視
  40.         Task.Run(Listener);
  41.     }
  42.  
  43.     public void InputText() {
  44.         //テキストにinputFieldの内容を反映
  45.         send_text = input_field.text;
  46.     }
  47.  
  48.     // ボタンクリック時
  49.     public void NetworkRequest() {
  50.         // 接続要求
  51.         network_stream = new TcpClient(server_ip_address, port).GetStream();
  52.  
  53.         // 送信の内容をセット
  54.         if (send_text.Length == 0) return;
  55.         byte[] send_string = Encoding.UTF8.GetBytes(send_text);
  56.  
  57.         // データを送信
  58.         network_stream.Write(send_string, 0, send_string.Length);
  59.  
  60.         // データを受信
  61.         //network_stream.Read(buffer, 0, buffer.Length);
  62.         //string message = Encoding.UTF8.GetString(buffer);
  63.         //GameObject instance = Instantiate(chat_log);
  64.         //instance.transform.parent = content.transform;
  65.         //instance.transform.localScale = new Vector3(1, 1, 1);
  66.         //instance.GetComponent<Text>().text = message;
  67.  
  68.         network_stream.Close();
  69.     }
  70.  
  71.  
  72.     public void Listener() {
  73.         listener = new TcpListener(IPAddress.Any, port);
  74.  
  75.         Debug.Log("監視開始1");
  76.  
  77.         try {
  78.             listener.Start();
  79.         }
  80.         catch (Exception e) {
  81.             Debug.Log(e.Message);
  82.             Debug.Log(e.TargetSite);
  83.             Debug.Log(e.StackTrace);
  84.         }
  85.  
  86.         Debug.Log("監視開始2");
  87.         while (true) {
  88.             //接続要求があったら受け入れる
  89.             TcpClient client = listener.AcceptTcpClient();
  90.  
  91.             Debug.Log("きた!");
  92.  
  93.             network_stream = client.GetStream();
  94.  
  95.             // データを受信
  96.             network_stream.Read(buffer, 0, buffer.Length);
  97.             string message = Encoding.UTF8.GetString(buffer);
  98.             Debug.Log(message);
  99.             GameObject instance = Instantiate(chat_log);
  100.             instance.transform.parent = content.transform;
  101.             instance.transform.localScale = new Vector3(1, 1, 1);
  102.             instance.GetComponent<Text>().text = message;
  103.  
  104.             // バッファリセット
  105.             buffer = new byte[4096];
  106.  
  107.             network_stream.Close();
  108.         }
  109.     }
  110.  
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top