开启服务器,客户端通过Socket来寻找服务器的IP地址,脚本直接过摄像机就能运行,打包。
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Chat : MonoBehaviour {
public string ip = "127.0.0.1";
public int port=4865;
private int connectCount = 15;
private bool useNAT=false;//是否启用网络地址转换器
private string recMes="";//接收到的消息
private string sendMes="";//要发送的消息
NetworkView netWorkView;
// Use this for initialization
void Start () {
netWorkView = GetComponent<NetworkView> ();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
switch (Network.peerType)
{
case NetworkPeerType.Disconnected://未连接,开始创建
StartCreat ();
break;
case NetworkPeerType.Server://服务器状态,启动服务器
OnServer ();
break;
case NetworkPeerType.Client://客户端状态 开启客户端
OnClient();
break;
case NetworkPeerType.Connecting://尝试连接
Debug.Log ("链接中。。。");
break;
}
}
/// <summary>
/// 开始创建服务器或者客户端
/// </summary>
void StartCreat()
{
GUILayout.BeginVertical ();
if (GUILayout.Button("新建服务器"))
{
NetworkConnectionError error = Network.InitializeServer (connectCount,port,useNAT);//初始化网络服务连接
Debug.Log (error);
serverIsRun=true;
StartServer ();
}
if (GUILayout.Button("连接服务器"))
{
clientIsRun = true;
StartClient ();
NetworkConnectionError error = Network.Connect (ip,port);//根据服务器IP和端口连接
Debug.Log(error);
}
GUILayout.EndVertical ();
}
private string strInfo;
Thread serverThread=null;//服务器线程
UdpClient UdpSend=null;//Udp网络传输
Thread clientThread=null;//客户端线程
UdpClient UdpListen=null;//客户端传输
bool serverIsRun=false;
bool clientIsRun=false;
const string specialText = "";
void StartServer()//服务器一直发消息
{
strInfo = "开始服务器...";//服务器信息
if (serverThread != null && serverThread.IsAlive) return;//如果服务器线程不等于空,并且服务器线程处于活动状态
serverThread = new Thread(() =>
{
UdpSend = new UdpClient();//服务器进行Udp网络传播
while (serverIsRun)
{
Thread.Sleep(500);//线程暂停0.5s
byte[] buf = Encoding.Unicode.GetBytes(specialText);//将特殊文本转换为字节流
UdpSend.Send(buf, buf.Length, new IPEndPoint(IPAddress.Broadcast, port));
}
UdpSend.Close();
});
serverThread.IsBackground = true;
InitServer();
serverThread.Start();
strInfo = "服务器已开启";
}
void StartClient()//客户端收消息直到收到服务器的IP
{
strInfo = "开始客户端...";
if (clientThread != null && clientThread.IsAlive) return;
clientThread = new Thread(() =>
{
UdpListen = new UdpClient(new IPEndPoint(IPAddress.Any, port));
while (clientIsRun)
{
Thread.Sleep(10);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);//表示IP和端口的类
byte[] bufRev = UdpListen.Receive(ref endpoint);//Udp监听到客户端的字节流
string msg = Encoding.Unicode.GetString(bufRev, 0, bufRev.Length);//客户端得到的字节流转换为字符串
if (msg.Contains(specialText))
{
strInfo = "我是客户端,接收到的IP地址为:" + endpoint.Address.ToString();
Debug.Log(strInfo);
ip=endpoint.Address.ToString();
UdpListen.Close();
return;
}
else
{
strInfo = "我是客户端,接收到的IP地址:" + endpoint.Address.ToString() + ",但是并没有用!";
ip=endpoint.Address.ToString();
}
}
UdpListen.Close();
});
clientThread.IsBackground = true;//后台线程为正
InitClient();
clientThread.Start();
strInfo = "接收...";
}
void InitServer()
{
serverIsRun = true;
if (UdpSend!=null)
{
UdpSend.Close ();
}
}
void InitClient()
{
clientIsRun = true;
if (UdpListen!=null)
{
UdpListen.Close ();
}
}
void StopServer()
{
serverIsRun = false;
if (UdpSend != null && serverThread.IsAlive)
serverThread.Abort ();
}
public void StopClient()
{
clientIsRun = false;
if (UdpListen != null)
UdpSend.Close ();
if (serverThread != null && clientThread.IsAlive)
clientThread.Abort ();
}
void OnDestroy()
{
StopClient ();
StopServer ();
Debug.Log ("Stop");
}
void OnServer()
{
GUILayout.Label ("新建服务器成功,等待客户端连接");
int length = Network.connections.Length;
for (int i = 0; i < length; i++)
{
GUILayout.Label ("连接的IP:"+Network.connections[i].ipAddress);
GUILayout.Label ("连接的端口:"+Network.connections[i].port);
GUILayout.Label ("Udp连接到IP"+ip);
}
if (GUILayout.Button("断开连接"))
{
Network.Disconnect ();
}
GUILayout.TextArea(recMes);//显示接收到的消息(只显示)
sendMes = GUILayout.TextField (sendMes);//输入消息
if (GUILayout.Button("发送消息"))
{
netWorkView.RPC ("SendMes", RPCMode.All, Network.player + "Say:" + sendMes);
}
}
void OnClient()
{
GUILayout.Label ("连接成功");
int length = Network.connections.Length;
for (int i = 0; i < length; i++)
{
GUILayout.Label ("连接的IP:"+Network.connections[i].ipAddress);
GUILayout.Label ("连接的端口:"+Network.connections[i].port);
GUILayout.Label ("Udp连接到IP"+ip);
}
if (GUILayout.Button("断开连接"))
{
Network.Disconnect ();
}
GUILayout.TextArea (recMes);
sendMes = GUILayout.TextField (sendMes);
if (GUILayout.Button("发送消息"))
{
netWorkView.RPC ("SendMes", RPCMode.All, Network.player + "说:" + sendMes);
}
}
[RPC]
void SendMes(string mes)
{
this.recMes+="\n";
this.recMes += mes;
}
}