本文整理汇总了C#中WebSocket.Recv方法的典型用法代码示例。如果您正苦于以下问题:C# WebSocket.Recv方法的具体用法?C# WebSocket.Recv怎么用?C# WebSocket.Recv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebSocket
的用法示例。
在下文中一共展示了WebSocket.Recv方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Play
public void Play()
{
var uri = new Uri(Server + "?user=" + Uri.EscapeDataString(UserName));
using (var socket = new WebSocket(uri))
{
socket.Connect();
while (!ShouldExit)
{
var response = socket.Recv();
if (!response.StartsWith(ResponsePrefix))
{
Console.WriteLine("Something strange is happening on the server... Response:\n{0}", response);
ShouldExit = true;
}
else
{
var boardString = response.Substring(ResponsePrefix.Length);
var action = DoMove(new GameBoard(boardString));
socket.Send(action);
}
}
}
}
示例2: Start
// Use this for initialization
IEnumerator Start () {
w = new WebSocket(new Uri("ws://ec2-54-213-60-9.us-west-2.compute.amazonaws.com:16248"));
w.OnError += OnError;
w.OnLogMessage += OnLogMessage;
Debug.Log("Trying to connect");
yield return StartCoroutine(w.Connect());
Debug.Log(w.isConnected ? "Connected!" : "Couldn't connect");
while (true) {
if (w.lastError != null)
break;
var reply = w.Recv();
if (reply != null) {
//var stringData = "[";
//for (var i = 0; i < reply.Length; ++i)
//{
// if (i % 4 == 0)
// stringData += "\n";
// var c = reply[i];
// stringData += " " + c.ToString("000");
//}
//stringData += " ]";
//Debug.Log("Received from the server: " + stringData);
uint spec = BitConverter.ToUInt32(reply, 0);
switch (spec) {
case specInitialize:
{
uint id = BitConverter.ToUInt32(reply, 4);
player.id = id;
uint otherPlayerCount = BitConverter.ToUInt32(reply, 8);
int byteIndex = 12;
for (int i = 0; i < otherPlayerCount; ++i)
{
var other = new Player();
other.id = BitConverter.ToUInt32(reply, byteIndex);
other.name = Encoding.Unicode.GetString(reply, byteIndex + 4, nameLength);
float x = BitConverter.ToSingle(reply, byteIndex + nameLength + 4);
float y = BitConverter.ToSingle(reply, byteIndex + nameLength + 8);
float z = BitConverter.ToSingle(reply, byteIndex + nameLength + 12);
other.pos = new Vector3(x, y, z);
other.health = BitConverter.ToSingle(reply, byteIndex + nameLength + 16);
otherPlayers[other.id] = other;
byteIndex += 4 + nameLength + 16;
}
Debug.Log("Received ID: " + id + " and game start info. There are " + otherPlayerCount + " other players.");
if (onGameInfoReceived != null)
onGameInfoReceived(id, otherPlayers);
break;
}
case specDisconnect:
{
uint id = BitConverter.ToUInt32(reply, 4);
Player other = null;
if (otherPlayers.TryGetValue(id, out other))
{
Debug.Log(other.name + " disconnected.");
if (onPlayerDisconnected != null)
onPlayerDisconnected(other);
} else
Debug.Log("player" + id + " disconnected.");
otherPlayers.Remove(id);
break;
}
case specAnnounceConnect:
{
uint id = BitConverter.ToUInt32(reply, 4);
if (id == player.id)
break;
Player other = new Player();
other.id = id;
other.name = Encoding.Unicode.GetString(reply, 8, nameLength);
float x = BitConverter.ToSingle(reply, 8 + nameLength);
float y = BitConverter.ToSingle(reply, 12 + nameLength);
float z = BitConverter.ToSingle(reply, 16 + nameLength);
other.pos = new Vector3(x, y, z);
otherPlayers[id] = other;
Debug.Log(other.name + " has connected (id " + other.id + ", pos " + other.pos + ")");
if (onPlayerConnected != null)
onPlayerConnected(other);
break;
}
case specUpdatePosition:
{
uint id = BitConverter.ToUInt32(reply, 4);
float x = BitConverter.ToSingle(reply, 8);
float y = BitConverter.ToSingle(reply, 12);
float z = BitConverter.ToSingle(reply, 16);
var pos = new Vector3(x, y, z);
Player other = null;
if (player.id == id)
break;
if (otherPlayers.TryGetValue(id, out other)) {
//Debug.Log(other.name + " is now at position " + pos);
other.pos = pos;
if (onPlayerMoved != null)
onPlayerMoved(other);
}
break;
}
case specMessage:
//.........这里部分代码省略.........