hmmmmmm
parent
a207658b77
commit
cb23d41691
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
|
|
@ -27,6 +26,7 @@ namespace VelNet
|
|||
YOU_LEFT = 7,
|
||||
ROOM_DATA = 8
|
||||
}
|
||||
|
||||
public enum MessageSendType
|
||||
{
|
||||
MESSAGE_OTHERS_ORDERED = 7,
|
||||
|
|
@ -131,7 +131,7 @@ namespace VelNet
|
|||
|
||||
public static bool IsConnected => instance != null && instance.connected && instance.udpConnected;
|
||||
|
||||
//this is for sending udp packets
|
||||
// this is for sending udp packets
|
||||
private static readonly byte[] toSend = new byte[1024];
|
||||
|
||||
// Use this for initialization
|
||||
|
|
@ -168,7 +168,7 @@ namespace VelNet
|
|||
public class RoomDataMessage : Message
|
||||
{
|
||||
public string room;
|
||||
public List<Tuple<int, string>> members = new List<Tuple<int, string>>();
|
||||
public readonly List<Tuple<int, string>> members = new List<Tuple<int, string>>();
|
||||
}
|
||||
|
||||
public class JoinMessage : Message
|
||||
|
|
@ -188,18 +188,19 @@ namespace VelNet
|
|||
public int masterId;
|
||||
}
|
||||
|
||||
public class YouJoinedMessage: Message
|
||||
public class YouJoinedMessage : Message
|
||||
{
|
||||
public List<int> ids;
|
||||
public string room;
|
||||
}
|
||||
|
||||
public class PlayerLeftMessage: Message
|
||||
public class PlayerLeftMessage : Message
|
||||
{
|
||||
public int userId;
|
||||
public string room;
|
||||
}
|
||||
public class YouLeftMessage: Message
|
||||
|
||||
public class YouLeftMessage : Message
|
||||
{
|
||||
public string room;
|
||||
}
|
||||
|
|
@ -296,8 +297,6 @@ namespace VelNet
|
|||
}
|
||||
case RoomsMessage rm:
|
||||
{
|
||||
Debug.Log("Got Rooms Message:\n" + rm);
|
||||
|
||||
try
|
||||
{
|
||||
RoomsReceived?.Invoke(rm);
|
||||
|
|
@ -312,7 +311,6 @@ namespace VelNet
|
|||
}
|
||||
case RoomDataMessage rdm:
|
||||
{
|
||||
Debug.Log("Got room data message:" + rdm.room);
|
||||
try
|
||||
{
|
||||
RoomDataReceived?.Invoke(rdm);
|
||||
|
|
@ -322,6 +320,7 @@ namespace VelNet
|
|||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case YouJoinedMessage jm:
|
||||
|
|
@ -331,17 +330,15 @@ namespace VelNet
|
|||
masterPlayer = null;
|
||||
|
||||
|
||||
foreach(int playerId in jm.ids)
|
||||
foreach (int playerId in jm.ids)
|
||||
{
|
||||
|
||||
VelNetPlayer player = new VelNetPlayer
|
||||
{
|
||||
isLocal = playerId == userid,
|
||||
room = jm.room,
|
||||
userid = playerId
|
||||
userid = playerId,
|
||||
isLocal = playerId == userid,
|
||||
};
|
||||
players.Add(player.userid, player);
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
|
|
@ -355,7 +352,9 @@ namespace VelNet
|
|||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
foreach(KeyValuePair<int,VelNetPlayer> kvp in players)
|
||||
foreach (KeyValuePair<int, VelNetPlayer> kvp in players)
|
||||
{
|
||||
if (kvp.Key != userid)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -367,12 +366,12 @@ namespace VelNet
|
|||
Debug.LogError(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
case YouLeftMessage lm:
|
||||
case YouLeftMessage msg:
|
||||
{
|
||||
string oldRoom = LocalPlayer?.room;
|
||||
// delete all networkobjects that aren't sceneobjects or are null now
|
||||
|
|
@ -401,6 +400,7 @@ namespace VelNet
|
|||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case PlayerLeftMessage lm:
|
||||
|
|
@ -446,11 +446,11 @@ namespace VelNet
|
|||
{
|
||||
Debug.LogError(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case JoinMessage jm:
|
||||
{
|
||||
|
||||
// we got a join message, create it
|
||||
VelNetPlayer player = new VelNetPlayer
|
||||
{
|
||||
|
|
@ -470,7 +470,6 @@ namespace VelNet
|
|||
}
|
||||
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
case DataMessage dm:
|
||||
|
|
@ -500,6 +499,7 @@ namespace VelNet
|
|||
{
|
||||
Debug.LogError("Scene Network ID is 0. Make sure to assign one first.", sceneObjects[i]);
|
||||
}
|
||||
|
||||
sceneObjects[i].networkId = -1 + "-" + sceneObjects[i].sceneNetworkId;
|
||||
sceneObjects[i].owner = masterPlayer;
|
||||
sceneObjects[i].isSceneObject = true; // needed for special handling when deleted
|
||||
|
|
@ -599,7 +599,6 @@ namespace VelNet
|
|||
AddMessage(new ConnectedMessage());
|
||||
while (true)
|
||||
{
|
||||
|
||||
//read a byte
|
||||
MessageReceivedType type = (MessageReceivedType)stream.ReadByte();
|
||||
|
||||
|
|
@ -658,6 +657,7 @@ namespace VelNet
|
|||
rdm.members.Add(new Tuple<int, string>(client_id, username));
|
||||
Debug.Log(username);
|
||||
}
|
||||
|
||||
AddMessage(rdm);
|
||||
break;
|
||||
}
|
||||
|
|
@ -696,10 +696,11 @@ namespace VelNet
|
|||
YouJoinedMessage m = new YouJoinedMessage();
|
||||
int N = GetIntFromBytes(ReadExact(stream, 4));
|
||||
m.ids = new List<int>();
|
||||
for(int i = 0; i < N; i++)
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
m.ids.Add(GetIntFromBytes(ReadExact(stream, 4)));
|
||||
}
|
||||
|
||||
N = stream.ReadByte();
|
||||
byte[] utf8data = ReadExact(stream, N); //the room name, encoded as utf-8
|
||||
m.room = Encoding.UTF8.GetString(utf8data);
|
||||
|
|
@ -725,7 +726,6 @@ namespace VelNet
|
|||
AddMessage(m);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -845,20 +845,22 @@ namespace VelNet
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects to the server with a username
|
||||
/// Connects to the server for a particular app
|
||||
/// </summary>
|
||||
public static void Login(string username, string password)
|
||||
/// <param name="appName">A unique name for your app. Communication can only happen between clients with the same app name.</param>
|
||||
/// <param name="deviceId">Should be unique per device that connects. e.g. md5(deviceUniqueIdentifier)</param>
|
||||
public static void Login(string appName, string deviceId)
|
||||
{
|
||||
MemoryStream stream = new MemoryStream();
|
||||
BinaryWriter writer = new BinaryWriter(stream);
|
||||
|
||||
byte[] uB = Encoding.UTF8.GetBytes(username);
|
||||
byte[] pB = Encoding.UTF8.GetBytes(password);
|
||||
byte[] id = Encoding.UTF8.GetBytes(deviceId);
|
||||
byte[] app = Encoding.UTF8.GetBytes(appName);
|
||||
writer.Write((byte)MessageSendType.MESSAGE_LOGIN);
|
||||
writer.Write((byte)uB.Length);
|
||||
writer.Write(uB);
|
||||
writer.Write((byte)pB.Length);
|
||||
writer.Write(pB);
|
||||
writer.Write((byte)id.Length);
|
||||
writer.Write(id);
|
||||
writer.Write((byte)app.Length);
|
||||
writer.Write(app);
|
||||
|
||||
SendTcpMessage(stream.ToArray());
|
||||
}
|
||||
|
|
@ -929,7 +931,7 @@ namespace VelNet
|
|||
SendToRoom(mem.ToArray(), include_self, reliable, ordered);
|
||||
}
|
||||
|
||||
public static void SendCustomMessageToGroup(string group, byte[] message,bool reliable = true)
|
||||
public static void SendCustomMessageToGroup(string group, byte[] message, bool reliable = true)
|
||||
{
|
||||
using MemoryStream mem = new MemoryStream();
|
||||
using BinaryWriter writer = new BinaryWriter(mem);
|
||||
|
|
@ -1049,7 +1051,7 @@ namespace VelNet
|
|||
writer.Write((byte)MessageType.Instantiate);
|
||||
writer.Write(newObject.networkId);
|
||||
writer.Write(prefabName);
|
||||
SendToRoom(mem.ToArray(), include_self:false, reliable:true);
|
||||
SendToRoom(mem.ToArray(), include_self: false, reliable: true);
|
||||
|
||||
return newObject;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue