Unity3D中的网络应用——聊天工具

Unity3D中的网络应用——简易聊天工具

服务器端编程:

服务器需要一个监听的端口:

private int connectPort=2224

判断服务器是否启动的方法

Network.peerType==NetworkPeerType.Disconnected

服务器的启动方法

Network.InitializeServer(32,connectPort,false);

客户端连接数组

Network.connections

断开连接

Network.Disconnect(200);

获得网速延时

Network.GetAveragePing(Network.connections[0]);

【代码】主要是开启服务器端

using UnityEngine;

using System.Collections;

using System.Collections.Generic; using System;

public class ChatServer : MonoBehaviour

{

private string chatWords = "";

private int connectPort = 10000; //本地端口

void OnGUI()

{

if (Network.peerType == NetworkPeerType.Disconnected)

{

http://m.sodocs.net/doc/300d1ec0f524ccbff121847e.html bel("服务器尚未启动");

connectPort = Convert.ToInt32(GUILayout.TextField(connectPort.ToString()));

if (GUILayout.Button("开启服务器"))

{

Network.InitializeServer(32, connectPort, false);

}

}

else if (Network.peerType == NetworkPeerType.Server)

{

http://m.sodocs.net/doc/300d1ec0f524ccbff121847e.html bel("服务器已经开启");

http://m.sodocs.net/doc/300d1ec0f524ccbff121847e.html bel("连接客户端数量:" + Network.connections.Length);

if (Network.connections.Length >= 1)

{

http://m.sodocs.net/doc/300d1ec0f524ccbff121847e.html bel("第一个客户端的ping值:" + Network.GetAveragePing(Network.connections[0]));

相关推荐
相关主题
热门推荐