热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UDP实现一个简易的聊天室(Unity&&C#完成)

效果展示(尚未完善)chatudpclienttestusingsystem.collections;usingsystem.collections.generic;using

  效果展示(尚未完善)


chatudpclienttest

using system.collections;
using system.collections.generic;
using unityengine;
using system.threading;
using system.net;
using system.net.sockets;
using system.text;
using common;
using uiwidgetssamples;
using system;
///


/// 服务端
///

public class chatudpservertest : monobehaviour
{
public string serverip;
//ip地址
public int serverport;
//端口
//1.创建scoket对象 ip port
private thread thread;
private udpclient udpseivic;
public void start()
{
chatview = transform.findchildbyname("chatview").
getcomponent();
//给端口和ip
//构建终结点 ip和一个端口
ipendpoint localep = new
ipendpoint(ipaddress.parse(serverip), serverport);
udpseivic = new udpclient(localep);
thread = new thread(receivemessage);
thread.start();
}

///
/// 接收消息
///

private void receivemessage()
{
while (true)
{
ipendpoint remote = new
ipendpoint(ipaddress.any, 0);
//创建任意终结点
//ref
byte[] date = udpseivic.receive(ref remote);
//receive接收消息 如果没有收到消息 线程阻塞 放在线程中
string msg = encoding.utf8.getstring(date);
//获取的客户都安信息
debug.log(remote.address + "===" + remote.port);
//如果接收客户端的消息,会把任意终结点修改为客户端的终结点
threadcrosshelper.instance.executeonmainthread(() => { showmessage(msg); });
}
}
private chatview chatview;
///
/// 显示消息
///

///


public void showmessage(string msg)
{
chatview.datasource.add(new chatline()
{
username = "annns",
message = msg,
time = datetime.now,
type = chatlinetype.user,
});
}
private void onapplicationquit()
{
udpseivic.close();
thread.abort();
}
}

chatudpservertest

脚本引用的工具箱



  1. monosingleton (泛型单例)

  2. threadcrosshelper (为子线程提供,可以在主线程中执行的方法)

  3. transformhelper(根据名称查找后代元素)

using system.collections;
using system.collections.generic;
using unityengine;
namespace common
{
///


///
///

public class monosingleton : monobehaviour where t : monosingleton
{
//public static t instance
//{
// get;
// private set;
//}
//private void awake()
//{
// instance = this as t;
//}
//按需加载
private static t instance;
public static t instance
{
get
{
if (instance == null)
{
//在场景中查找对象
instance = findobjectoftype();
if (instance == null)
{
//创建游戏对象 附加 脚本对象
new gameobject("singleton of " + typeof(t)).addcomponent();//立即执行awake
}
else
{
instance.initialized();
}
}
return instance;
}
}
protected virtual void initialized()
{
}
[tooltip("是否需要跨场景不销毁")]
public bool isdOntdestroy= true;
//如果管理类自行附加到物体中
//在awake中为instance赋值
protected void awake()
{
if (isdontdestroy)
{
dontdestroyonload(gameobject);
}
if (instance == null)
{
instance = this as t;
instance.initialized();
}
}
}
}

monosingleton

using system;
using system.collections;
using system.collections.generic;
using unityengine;

namespace common
{
///


///
///

public class threadcrosshelper : monosingleton
{
///
/// 延迟项
///

class delayeditem
{
public action currentaction { get; set; }
public datetime time { get; set; }
}
private list actionlist;
//private list actionlist;
//private list timelist;
protected override void initialized()
{
base.initialized();
actiOnlist= new list();
}
private void update()
{
for (int i = actionlist.count - 1; i >= 0; i--)
{
//到时间
if (actionlist[i].time <= datetime.now)
{
lock (actionlist)
{
actionlist[i].currentaction();//执行
actionlist.removeat(i);//从列表中移除
}
}
}
}
///
/// 为子线程提供,可以在主线程中执行的方法
///

///


///


public void executeonmainthread(action action, float dealy = 0)
{
delayeditem item = new delayeditem()
{
currentaction = action,
//time = time.time + dealy
time = datetime.now.addseconds(dealy)
};
lock (actionlist)
{
actionlist.add(item);
}
}
}
}

threadcrosshelper

using system.collections;
using system.collections.generic;
using unityengine;
namespace common
{
///


/// 变换组件助手类
///

public static class transformhelper
{
///
/// 未知层级,根据名称查找后代元素
///

///


///


///
public static transform findchildbyname(this transform currenttf, string childname)
{
transform childtf = currenttf.find(childname);
if (childtf != null) return childtf;
//将问题推迟给子物体
for (int i = 0; i {
//在方法体内部,又遇到了相同的问题,所以需要调用自身。
childtf = findchildbyname(currenttf.getchild(i), childname);
if (childtf != null) return childtf;
}
return null;
}
}
}

transformhelper



推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 突破MIUI14限制,自定义胶囊图标、大图标样式,支持任意APP
    本文介绍了如何突破MIUI14的限制,实现自定义胶囊图标和大图标样式,并支持任意APP。需要一定的动手能力和主题设计师账号权限或者会主题pojie。详细步骤包括应用包名获取、素材制作和封包获取等。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • LwebandStringTimeLimit:20001000MS(JavaOthers)MemoryLimit:6553665536K(JavaO ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 本文介绍了Sencha Touch的学习使用心得,主要包括搭建项目框架的过程。作者强调了使用MVC模式的重要性,并提供了一个干净的引用示例。文章还介绍了Index.html页面的作用,以及如何通过链接样式表来改变全局风格。 ... [详细]
author-avatar
VW旻shi只吃货8453
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有