コード例 #1
0
 public void SendMessage(string message, SocketEventHandler OnReceiveMessage)
 {
     if (IsLogined == true)
     {
         socketManager.SendData(loginSocket, user_id + "#" + message, OnReceiveMessage);
     }
 }
コード例 #2
0
    void Start()
    {
        Debug.Log("das");

        socketEventHandler = new SocketEventHandler(this);

        if (SharedArea.id != -1 && !SharedArea.isLoggedIn)
        {
            Debug.Log("ID: " + SharedArea.id + "\t\tNickname: " + SharedArea.nickname);
            SharedArea.socketClient = new SocketClient();
            SharedArea.socketClient.SetSocketEventListener(socketEventHandler);
            SharedArea.socketClient.Connect(Config.SERVER_IP, Config.GAME_SERVER_PORT);
            StartCoroutine(SharedArea.socketClient.DataReceiveCorutine());
            StartCoroutine(SharedArea.socketClient.DataSendCorutine());

            LoginToGameServer();
        }
        else if (SharedArea.isLoggedIn)
        {
            SharedArea.socketClient.SetSocketEventListener(socketEventHandler);
            StartCoroutine(SharedArea.socketClient.DataReceiveCorutine());
            StartCoroutine(SharedArea.socketClient.DataSendCorutine());

            RequestUserList();
            RequestRoomList();
        }
    }
コード例 #3
0
 public int Connect(string ip, int port, SocketEventHandler handler)
 {
     try {
         m_ip              = ip;
         m_port            = port;
         this.eventHandler = handler;
         IPAddress  ipAddress  = Dns.GetHostEntry(ip).AddressList[0];
         IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port);
         client.Connect(ipEndPoint);
         //error_id = NETWORK_CONNECTED;
         BinaryPack header = new BinaryPack(PACKET_HEAD_SIZE);
         client.GetStream().BeginRead(header.buffer, 0, PACKET_HEAD_SIZE, new AsyncCallback(DoReadPacketHeader), header);
         return(0);
     }
     catch (SocketException ex) {
         error_id = NETWORK_CONNECT_ERROR;
         res      = ex.Message;
         return(-1);
     }
     catch (System.Security.SecurityException se) {
         error_id = NETWORK_CONNECT_ERROR;
         res      = se.Message;
         return(-1);
     }
 }
コード例 #4
0
ファイル: NetworkManager.cs プロジェクト: niuniuzhu/RC
        public static void AddServerEventHandler(string serverName, SocketEventHandler socketEventHandler)
        {
            INetServer server = GetServer(serverName);

            if (server != null)
            {
                server.OnSocketEvent += socketEventHandler;
            }
        }
コード例 #5
0
        private void DoPong(SocketEventArgs e)
        {
            SocketEventHandler handler = OnPong;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #6
0
     public void On(string eventname, SocketEventHandler handler)
     {
         _handlers[eventname] = handler;
 #if UNITY_EDITOR || UNITY_STANDALONE
         _socket.On(eventname, OnRawEvent);
 #else
         Application.ExternalCall("OnEvent", _objectName, eventname, handler.Method.Name);
 #endif
     }
コード例 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected void TriggerClosed(SocketEventArgs e)
        {
            SocketEventHandler handler = OnClosed;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #8
0
        void OnRawEvent(SocketIOEvent e)
        {
            SocketEventHandler h = _handlers[e.name];

            if (h != null)
            {
                h(e.data.ToString());
            }
        }
コード例 #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected void TriggerMessage(SocketEventArgs e)
        {
            SocketEventHandler handler = OnMessage;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #10
0
ファイル: NetworkManager.cs プロジェクト: niuniuzhu/RC
        public static void AddClientEventHandler(string clientName, SocketEventHandler socketEventHandler)
        {
            INetClient client = GetClient(clientName);

            if (client != null)
            {
                client.OnSocketEvent += socketEventHandler;
            }
        }
コード例 #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected virtual void TriggerError(SocketEventArgs e)
        {
            SocketEventHandler handler = OnError;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #12
0
        private void OnConstructing(TcpClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.RemoteEndPoint == null)
            {
                throw new ArgumentNullException(nameof(options.RemoteEndPoint));
            }

            connection = new TcpConnection(options.RemoteEndPoint, options.LocalEndPoint)
            {
                OnConnected = OnConnectedHandler, OnReceived = OnReceivedHandler, OnDisconnected = OnDisconnectedHandler, OnStarted = OnStartedHandler, OnStopped = OnStoppedHandler, OnException = OnExceptionHandler, ConnectTimeout = options.ConnectTimeout, OnTryConnecting = OnTryConnectingHandler, ReconnectEnable = options.ReconnectEnable, TryConnectionStrategy = options.TryConnectionStrategy
            };

            connection.KeepAlive(KeepAliveOption.KeepAlive, KeepAliveOption.Interval, KeepAliveOption.Span);

            if (options.Id > 0)
            {
                connection.Id = options.Id;
            }
            if (!options.Name.IsNullOrWhiteSpace())
            {
                connection.Name = options.Name;
            }

            if (options.OnConnected != null)
            {
                this.OnConnected = options.OnConnected;
            }
            if (options.OnReceived != null)
            {
                this.OnReceived = options.OnReceived;
            }
            if (options.OnDisconnected != null)
            {
                this.OnDisconnected = options.OnDisconnected;
            }
            if (options.OnStarted != null)
            {
                this.OnStarted = options.OnStarted;
            }
            if (options.OnStopped != null)
            {
                this.OnStopped = options.OnStopped;
            }
            if (options.OnException != null)
            {
                this.OnException = options.OnException;
            }
            if (options.OnTryConnecting != null)
            {
                this.OnTryConnecting = options.OnTryConnecting;
            }
        }
コード例 #13
0
    public void Process(int eventID, NetInfo obj)
    {
        if (eventID == 0)
        {
            return;
        }

        ProcessEvent(eventID, obj);
        SocketEventHandler.ProcEvent((uint)eventID, obj);
    }
コード例 #14
0
    void Update()
    {
        if (SharedArea.isLoggedIn)
        {
            socketEventHandler = new SocketEventHandler(this);

            SharedArea.socketClient.SetSocketEventListener(socketEventHandler);
            StartCoroutine(SharedArea.socketClient.DataReceiveCorutine());
            StartCoroutine(SharedArea.socketClient.DataSendCorutine());
        }
    }
コード例 #15
0
 public void UnRegistEventHandler(ESocketEventType eventType, SocketEventHandler callback)
 {
     if (mSocketEventDic != null && mSocketEventDic.ContainsKey((int)eventType))
     {
         mSocketEventDic[(int)eventType] -= callback;
         if (mSocketEventDic[(int)eventType] == null)
         {
             mSocketEventDic.Remove((int)eventType);
         }
     }
 }
コード例 #16
0
 private void InvokeSocketEvent(int eventType, string value)
 {
     if (mSocketEventDic != null && mSocketEventDic.ContainsKey(eventType))
     {
         SocketEventHandler callback = mSocketEventDic[eventType];
         if (callback != null)
         {
             callback.Invoke(value);
         }
     }
 }
コード例 #17
0
ファイル: NetworkHost.cs プロジェクト: cspanier/shift
        internal void QueueEvent(SocketEventHandler socketEvent)
        {
            _eventQueue.Enqueue(socketEvent);
            _eventCount.Release();
            var bufferAvailable = BufferAvailable;

            if (bufferAvailable != null)
            {
                bufferAvailable(this, new BufferAvailableArgs());
            }
        }
コード例 #18
0
        public void Connect(Socket socket, string address, int port, SocketEventHandler OnConnected)
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(address), port);

            socket.BeginConnect(ipep, (s) =>
            {
                OnConnected(200);
                Debug.WriteLine(ipep.Address + ":" + ipep.Port + " 서버 연결됨");
            }, socket);
            Debug.WriteLine(address + "서버 연결 시작");
        }
コード例 #19
0
    void Start()
    {
        socketEventHandler = new SocketEventHandler(this);

        socketClient = new SocketClient();
        socketClient.SetSocketEventListener(socketEventHandler);
        ConnectToServer();

        StartCoroutine(socketClient.DataReceiveCorutine());
        StartCoroutine(socketClient.DataSendCorutine());
    }
コード例 #20
0
        /// <summary>
        /// 소켓 인증하기
        /// </summary>
        /// <param name="tokenInfo">사용자 TokenInfo</param>
        /// <returns></returns>
        //public void Authentication(TokenInfo tokenInfo)
        //{
        //    var data = new JObject();
        //    data["token"] = tokenInfo.Token;
        //    data["device"] = "pc";

        //     Emit("authentication", data);
        //    Debug.WriteLine("소켓 인증시도중");
        //}

        /// <summary>
        /// 소켓이 인증 성공 및 연결 유지
        /// </summary>
        /// <param name="socketEventHandler"></param>
        /// <returns></returns>
        //public void Authenticated(SocketEventHandler socketEventHandler)
        //{
        //     EventOn("authenticated", (s, e) =>
        //    {
        //        Debug.WriteLine("소켓 인증됨");
        //        socketEventHandler(s, e);
        //    });
        //}

        /// <summary>
        /// 소켓 인증 실패 및 연결 해제
        /// </summary>
        /// <param name="socketEventHandler"></param>
        /// <returns></returns>
        //public void Disconnect(SocketEventHandler socketEventHandler)
        //{
        //    EventOn("disconnect", (s, e) =>
        //    {
        //        Debug.WriteLine("소켓 인증 실패");
        //        socketEventHandler(s, e);
        //    });
        //}


        /// <summary>
        /// 소켓 이벤트 생성
        /// </summary>
        /// <param name="eventString">요청받을 이벤트 이름</param>
        /// <param name="OnDataGetEnded">요청받은 후 발생시킬 이벤트</param>
        // TODO : Warning 제거
        public void EventOn(Socket socket, string eventString, SocketEventHandler OnDataGetEnded)
        {
            socket.On(eventString, (data) =>
            {
                if (data != null)
                {
                    if (OnDataGetEnded != null)
                    {
                        OnDataGetEnded(this, data.ToString());
                    }
                }
            });
        }
コード例 #21
0
 public void RegistSocketEventHandler(ESocketEventType eventType, SocketEventHandler callback)
 {
     if (mSocketEventDic != null)
     {
         if (mSocketEventDic.ContainsKey((int)eventType))
         {
             mSocketEventDic[(int)eventType] += callback;
         }
         else
         {
             mSocketEventDic.Add((int)eventType, callback);
         }
     }
 }
コード例 #22
0
    void Start()
    {
        if (SharedArea.isLoggedIn)
        {
            socketEventHandler = new SocketEventHandler(this);

            SharedArea.socketClient.SetSocketEventListener(socketEventHandler);
            StartCoroutine(SharedArea.socketClient.DataReceiveCorutine());
            StartCoroutine(SharedArea.socketClient.DataSendCorutine());

            RequestRoomChiefData();
            RequestRoomMemberList();
        }
    }
コード例 #23
0
 public TcpClientOptions(IPEndPoint remoteEndPoint, IPEndPoint localEndPoint = null, SocketEventHandler <NetClientReceivedEventArgs <ITcpConnection> > handler = null)
 {
     if (localEndPoint != null)
     {
         LocalEndPoint = localEndPoint;
     }
     if (remoteEndPoint != null)
     {
         RemoteEndPoint = remoteEndPoint;
     }
     if (handler != null)
     {
         OnReceived = handler;
     }
 }
コード例 #24
0
        public void SendData(Socket socket, string data, SocketEventHandler OnSendCompleted)
        {
            if (IsConnected(socket) == false)
            {
                OnSendCompleted(500);
                return;
            }

            byte[] bytes = Encoding.UTF8.GetBytes(data);
            socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, (s) =>
            {
                OnSendCompleted(200);
                Debug.WriteLine(data + "전송완료");
            }, socket);
        }
コード例 #25
0
ファイル: SocketManager.cs プロジェクト: MAR1026/CheerUp
        public void EventOn(string eventName, SocketEventHandler OnDataGetEnded)
        {
            socket.On(eventName, (data) =>
            {
                if (data != null)
                {
                    Debug.WriteLine(data);

                    if (OnDataGetEnded != null)
                    {
                        OnDataGetEnded(this, data);
                    }
                }
            });
        }
コード例 #26
0
 public TcpServerOptions(IPEndPoint localEndPoint, int backlog = int.MaxValue, AddressFamily?family = null, SocketEventHandler <NetClientReceivedEventArgs <ITcpConnection> > handler = null)
 {
     LocalEndPoint = localEndPoint;
     if (backlog > 0)
     {
         Backlog = backlog;
     }
     if (family != null)
     {
         Family = family.Value;
     }
     if (handler != null)
     {
         OnReceived = handler;
     }
 }
コード例 #27
0
        /// <summary>
        /// Initializes the UDP server and automatically starts waiting for connections on all available local IP addresses.
        /// </summary>
        /// <param name="port">The local port to listen to</param>
        /// <param name="newClientCallback">The method to call if a client has been found</param>
        /// <param name="receivedPacketCallback">The method to call if a new packet has been received</param>
        public UdpServer(int port, SocketEventHandler newClientCallback, DataEventHandler receivedPacketCallback)
        {
            // Initialize the listener socket
            _listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                ExclusiveAddressUse = false, EnableBroadcast = true
            };
            // Start listening to any connection made to the specified port
            _listener.Bind(new IPEndPoint(IPAddress.Any, port));

            ClientConnectionRequested += newClientCallback;
            ReceivedPacket            += receivedPacketCallback;

            Active = true;
            Receive();
        }
コード例 #28
0
        /// <summary>
        /// Initializes the UDP server and automatically starts waiting for connections.
        /// </summary>
        /// <param name="hostname">The local IP to listen to</param>
        /// <param name="port">The local port to listen to</param>
        /// <param name="newClientCallback">The method to call if a client has been found</param>
        /// <param name="receivedPacketCallback">The method to call if a new packet has been received</param>
        public UdpServer(string hostname, int port, SocketEventHandler newClientCallback, DataEventHandler receivedPacketCallback)
        {
            if (!IPAddress.TryParse(hostname, out IPAddress ip))
            {
                throw new InvalidCastException($"Could not convert {hostname} to a valid IPAddress instance.");
            }

            // Initialize the listener socket
            _listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                ExclusiveAddressUse = false, EnableBroadcast = true
            };
            // Start listening to the specified IP and port
            _listener.Bind(new IPEndPoint(ip, port));

            ClientConnectionRequested += newClientCallback;
            ReceivedPacket            += receivedPacketCallback;

            Receive();
        }
コード例 #29
0
        public void Login(string id, SocketEventHandler OnLoginCompleted)
        {
            string Id = "@" + id;

            socketManager.SendData(loginSocket, Id, OnLoginCompleted);
        }
コード例 #30
0
ファイル: frmMain.cs プロジェクト: hkiaipc/lx
        /// <summary>
        /// ���幤�������˵�����, ���ڳ�ʼ��
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, System.EventArgs e)
        {
            //
            //
            this.mnuTest.Visible = Config.Default.IsTest;

            //��ʾ���вɼ���
            Gate_ListView_Ini(this.listView_Gate);

            Pump_ListView_Ini(this.listView_Pump);

            SetStatusBarText("���¹�����ˮ���Ȳɼ�ϵͳ");

            //string[] newpumps = new NewPumpReader().Read();
            //if ( newpumps != null )
            //{
            //    SetCsinfoCommunicationProtocol( InfoList_All, newpumps );
            //}

            _socketEvent += new SocketEventHandler(frmMain__socketEvent);

            threadListen = new Thread(new ThreadStart(ListeningThread));
            threadListen.Start();

            StartReceiveThread();
            //StartUdpServerThread();

            // start gprs coll
            //
            if (!Run_Stp_Order)
                SwitchRun();

            //_closeConfirm = true;

            // start importer
            //
            _importer = new Importer();
            _importer.Start();
        }