示例#1
0
    static void Main(string[] args)
    {
        try
        {
            SharpClient client = new SharpClient();
            client.SetChannel("test_1");

            client.OnConnect = () =>
            {
                Console.WriteLine("OnConnect");
                string msg = "hello from sharpclient";
                client.Write(Encoding.UTF8.GetBytes(msg));
            };
            client.OnClose = () =>
            {
                Console.WriteLine("close now");
            };
            client.OnMessage = (byte[] msg) =>
            {
                Console.WriteLine("read :" + Encoding.UTF8.GetString(msg));
            };


            //client.Connect("jgate.qipai.io", 18666);//公网
            client.Connect("localhost", 18666);//内网
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.ReadKey();
    }
示例#2
0
文件: Program.cs 项目: asciima/JGate
    static void Main(string[] args)
    {
        try
        {
            SharpClient client = new SharpClient();
            client.OnConnect = () =>
            {
                Console.WriteLine("OnConnect");
                string msg = "hello from sharpclient";
                client.Write(Encoding.UTF8.GetBytes(msg));
            };
            client.OnClose = () =>
            {
                Console.WriteLine("close now");
            };
            client.OnMessage = (byte[] msg) =>
            {
                Console.WriteLine("read :" + Encoding.UTF8.GetString(msg));
            };



            client.Connect("localhost", 28800);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.ReadKey();
    }
示例#3
0
        void S_OnClientConnected(SharpClient client)
        {
            #region Broadcasting To Other clients
            SharpSerializer ser = SharpSerializer.Create();
            ser.Write((byte)SharpConnectionMessages.ClientConnected);
            client.Write(ref ser);

            Broadcast(ser.DataAndPost(), client);
            #endregion

            SharpNetworking.Instance._OnClientConnected(client);
        }
示例#4
0
    // Use this for initialization
    void Start()
    {
        try
        {
            client.OnConnect = () =>
            {
                UnityThread.Current.Queue(() =>
                {
                    Debug.Log("OnConnect");
                    string msg = "hello from sharpclient";
                    client.Write(Encoding.UTF8.GetBytes(msg));
                }
                                          );
            };
            client.OnClose = () =>
            {
                UnityThread.Current.Queue(() => {
                    Debug.Log("close now");
                });
            };
            client.OnMessage = (byte[] msg) =>
            {
                UnityThread.Current.Queue(() => {
                    Debug.Log("read :" + Encoding.UTF8.GetString(msg));
                });
            };

            Debug.Log("connect now");

            client.Connect("localhost", 28800);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }
示例#5
0
        public bool Connect(string IP, int Port, string Password, SharpClient Self, out ConnectResults result)
        {
            if (Password == null)
            {
                Password = "";
            }
            if (this.connected)
            {
                result = ConnectResults.AlreadyConnected;
                return(false);
            }
            try
            {
                server_client = new TcpClient();
                IPAddress address = null;
                #region IP Parse
                try
                {
                    address = IPAddress.Parse(IP);
                }
                catch
                {
                    result = ConnectResults.InvalidIpAddress;
                    return(false);
                }
                #endregion
                IPEndPoint remoteAddress = new IPEndPoint(address, Port);

                #region Physical Connetion
                try
                {
                    server_client.Connect(remoteAddress);
                }
                catch
                {
                    result = ConnectResults.UnhandledException;
                    return(false);
                }
                #endregion

                #region ID Self
                SharpSerializer hailMessage = SharpSerializer.Create();

                hailMessage.Write((byte)IncomingConnectionRequests.ConnectionApprove);
                Self.Write(ref hailMessage);
                hailMessage.Write(Password);

                TCPMessageHandler.Write(server_client, hailMessage.DataAndPost());
                #endregion

                #region Wait For Response
                byte[]          responseRaw = TCPMessageHandler.Read(server_client);
                SharpSerializer response    = SharpSerializer.Create(responseRaw);
                #endregion

                IncomingConnectionResponse message = (IncomingConnectionResponse)response.ReadByte();
                if (message == IncomingConnectionResponse.WrongPassword)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.InvalidPassword;
                    return(false);
                }
                if (message == IncomingConnectionResponse.ServerIsFull)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.ServerIsFull;
                    return(false);
                }
                if (message == IncomingConnectionResponse.NameIsExist)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.NameIsExist;
                    return(false);
                }


                short myID = response.ReadInt16();
                Self.AssingNetID(myID);

                this.room = new SharpRoom(ref response);

                this.listenerThread = new Thread(ListenServer);
                this.listenerThread.Start();

                connected = true;

                me = Self;
                room.AssignClient(me);

                room.Server.Connection      = server_client;
                room.Server.ListeningThread = listenerThread;

                C_OnConnected(room);

                result = ConnectResults.Succes;
                return(true);
            }
            catch (Exception e)
            {
                try
                {
                    TCPMessageHandler.CloseConnection(server_client);
                }
                catch { }
                server_client = null;

                connected = false;

                try
                {
                    listenerThread.Abort();
                }
                catch { }
                listenerThread = null;

                me = null;

                room = null;

                result = ConnectResults.UnhandledException;
                return(false);
            }
        }