码迷,mamicode.com
首页 > 编程语言 > 详细

u3d 多线程 网络

时间:2014-12-23 12:07:56      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

开启一个线程做网络连接,和接收数据, 用event进行广播

using UnityEngine;
using System.Collections;
using System.Threading;
//using System.Threading.Tasks;
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;

public class Alpha
{

    public string v;

    public List<string> bytelist = new List<string>();

    public static event Action<String> handler;
    
    public void Beta()
    {
        GameNet gamenet = new GameNet();
        gamenet.start();
        gamenet.send("1");

        while(true){
            try{
                v = gamenet.conrecv();
                bytelist.Add(v);

                gamenet.send ("1");
            } catch(SocketException e){
                v = "socket closed";
                break;
            }
            if(handler != null){
                handler(bytelist[0]);
            }

            bytelist.Clear();

        }
    }
};

public class thread : MonoBehaviour {
    Alpha oAlpha = new Alpha();
    Thread oThread;


    void Start () {

        oThread = new Thread(new ThreadStart(oAlpha.Beta));
        oThread.Start();

        Alpha.handler += (s) => print ("client handler");
    }

    void Update () {

    }

    void OnDestory(){
        oThread.Abort();
    }
}

 

u3d 多线程 网络

标签:

原文地址:http://www.cnblogs.com/lightlfyan/p/4179693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!