1、字符串数据
简单的字符串:比如登录请求信息,登录结果返回的信息。
用json系列化的字符串:比如上传一个表到服务器,让它写入到数据库中。
读取文件的时候,读取的是string内容。
2、二进制数据
比如传输的是文件:例如myword.doc,myexcel.xls或者是assetboundle文件。
比如上传实验报告,生成实验报告并下载等。
读取文件的时候,直接读取字节码。
var jsonString = JsonUtility.ToJson(myScores); //myScores 预先定义的得分信息
websocket.SendText($"JSONSCOR{jsonString}");
///
/// 命令字符串
///
/// 命令字符串
///
public async UniTask SendCommand(string cmdText)
{//传输时自动在前面添加[COMD]var msg = $"COMD{cmdText}";await websocket.SendText(msg);
}
///
/// 发送文件
///
/// file.extend
/// byte[]数据
/// 成功与否
public async UniTask SendBinary(string fileName,byte[] data)
{Debug.Log($"即将发送数据:{fileName} {data.Length}");var rtn = false;if (fileName.Length > 30){Debug.Log("文件名不能超过30个字符");rtn = false;}else{var head = Encoding.UTF8.GetBytes("BINA"); var fileNameBytes = Encoding.UTF8.GetBytes(fileName.PadRight(30)); var allData = new List(); allData.AddRange(head); //头allData.AddRange(fileNameBytes); //文件名称allData.AddRange(data); //文件数据await websocket.Send(allData.ToArray());rtn = true;}return true;
}
//下载文件
downLoadBtn.onClick.AddListener(async () =>
{//【1】文件名var file = "shiYanBaoGao.docx";Debug.Log("从服务器下载文件");//COMD + down + # + shiYanBaoGao.docx//【2】请求下载实验报告var cmdText = $"COMDdown#{file}"; //[COMD][down][#][shiYanBaoGao.docx]await Connection.instance.websocket.SendText(cmdText);//【3】等待接收实验报告var data = await Connection.instance.ReceiveBinaryAsync(file);//【4】保存文件Debug.Log($"收到文件{file},大小为{data.Length}");FileSave.instance.SaveFileBytes(file,data);
});
客户端申请下载某个文件的时候逻辑:
任务列表的定义:
///
/// 下载任务列表: 客户端申请下载某个文件的时候使用。
/// private List DownFileTasks = new List();
///
[Serializable]
public class DownFileTask
{/// /// 任务ID/// public int taskID;/// /// 要下载的文件的名字:/// public string fileName;/// /// 下载状态:false-未下载,true-下载成功/// public bool state;/// /// 文件的数据:二进制信息/// public List data;
}
异步等待下载文件:
///
/// 等待接收文件
///
/// 文件名
///
public async UniTask ReceiveBinaryAsync(string fileName)
{//【1】创建下载任务var taskId = CreatTask(fileName, ref DownFileTasks);//【2】OnMessage(bytes[])+ ()=>{}中更新,收到文件,写入数据//在ParseBytes()里Debug.Log(DownFileTasks.Where(x => x.taskID == taskId).All(x => x.state));Debug.Log("开始等待下载......");//【3】等待下载await UniTask.WaitUntil(() => DownFileTasks.Where(x => x.taskID == taskId).All(x => x.state) == true);//【4】提取数据Debug.Log("提取数据......");var data = DownFileTasks.First(x => x.taskID == taskId).data;//【5】删除下载任务Debug.Log("删除下载任务......");DeleteTask(taskId, ref DownFileTasks);//【6】返回数据return data.ToArray();
}
///
/// 连接服务器
///
/// 服务器ip
/// 服务器端口号
///
async UniTask ConnectServer(string ip, int port)
{bool rtn = false;try{//websocket = new WebSocket("ws://192.168.0.137:8081");Debug.Log($"ws://{ip}:{port}");websocket = new WebSocket($"ws://{ip}:{port}");//连接打开时...websocket.OnOpen += () =>{hasConnected = true;userID = iptfdUsername.text;Debug.Log("Connection open!");textLog.text = $"Connection open! {Time.realtimeSinceStartup} {Environment.NewLine} {textLog.text}";websocket.SendText($"COMDname#{userID}"); //发送用户id};//连接出错时...websocket.OnError += (e) =>{Debug.Log("Error! " + e);textLog.text = $"Error:{e} {Time.realtimeSinceStartup} {Environment.NewLine} {textLog.text}";};//连接关闭时...websocket.OnClose += (e) =>{hasConnected = false;Debug.Log("连接被关闭了!");textLog.text = $"连接被关闭了! {Time.realtimeSinceStartup} {Environment.NewLine} {textLog.text}";};//收到二进制数据时...websocket.OnMessage += (bytes) =>{//解析数据ParseBytes(bytes);};//开始连接await websocket.Connect();rtn = true;}catch (Exception e){Debug.Log($"连接服务器出错:{e.Message}");textLog.text = $"连接服务器出错:{e.Message}";rtn = false;}return rtn;
}
//var server = new WebSocketServer("ws://192.168.0.137:8081");
var server = new WebSocketServer($"ws://{ip}:{port}");
server.Start(socket =>
{//连上sssocket.OnOpen = () =>{this.Invoke(new Action(() =>{//textBoxLog.Text += $"有新用户连入:{socket.ConnectionInfo.ClientIpAddress} {Environment.NewLine} {textBoxLog.Text}";AddText($"有新用户连入:{socket.ConnectionInfo.ClientIpAddress}");}));//报错的写法//textBoxLog.Text = $"有新用户连入:{socket.ConnectionInfo.ClientIpAddress} \n {textBoxLog.Text}";//SetTextLog($"有新用户连入:{socket.ConnectionInfo.ClientIpAddress}");Debug.WriteLine($"有新用户连入:{socket.ConnectionInfo.ClientIpAddress}");//回复一个信息//SendCommand(socket);};//断开socket.OnClose = () =>{Debug.WriteLine($"用户断开连接:{socket.ConnectionInfo.ClientIpAddress}");this.Invoke(new Action(() =>{AddText($"用户断开连接:{socket.ConnectionInfo.ClientIpAddress}");}));UserSockets.First(x => x.socket.ConnectionInfo.Id == socket.ConnectionInfo.Id).connected = false;};//收到string信息socket.OnMessage = message =>{Debug.WriteLine($"收到一条消息,来自:{socket.ConnectionInfo.ClientIpAddress}");Debug.WriteLine(message);this.Invoke(new Action(() =>{AddText($"收到一条消息,来自:{socket.ConnectionInfo.ClientIpAddress}");AddText($"{message}");}));//解析客户端字符串命令ParseString(message,socket);};//收到二进制信息socket.OnBinary = bytes =>{var userName = UserSockets.First(x => x.socket.ConnectionInfo.Id == socket.ConnectionInfo.Id).userID;Debug.WriteLine($"收到二进制数据,长度为{bytes.Length}Bytes,来自ip:{socket.ConnectionInfo.ClientIpAddress},userID ={userName}");this.Invoke(new Action(() =>{AddText($"收到二进制数据,长度为{bytes.Length}Bytes,来自ip:{socket.ConnectionInfo.ClientIpAddress},userID ={userName}");}));var head = Encoding.UTF8.GetString(bytes.Take(4).ToArray());switch (head){case "BINA"://收到二进制文件ParseBinaryFile(bytes,socket);break;case "other"://break;default://break;}};
});
实时传输语音和视频