//using Tcp as Connect protocol public void Connect(string serverIp, int serverPort, ConnectCallback connectCallback, ConnectCallback connectFailedCallback ) { mConnectCallback = connectCallback; mConnectFailedCallback = connectFailedCallback; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(serverIp); IPEndPoint endpoint = new IPEndPoint(address, serverPort); IAsyncResult result = socket.BeginConnect(endpoint, new AsyncCallback(ConnectedCallback), socket); //timeout detecting bool success = result.AsyncWaitHandle.WaitOne(mTimeOut, true); if (!success) { //time out occurs mConnectFailedCallback?.Invoke(); } else { StartReceive(); } }
public void Connect(ConnectCallback callback) { ws = new WebSocket(websocketURL); var cookie = new Cookie(); cookie.Name = "access"; cookie.Value = PlayerPrefs.GetString("uid"); ws.SetCookie(cookie); ws.OnOpen += OnOpenHandler; ws.OnMessage += OnMessageHandler; ws.OnClose += OnCloseHandler; stateMachine.AddHandler(APIState.Running, () => { ws.ConnectAsync(); }); stateMachine.AddHandler(APIState.Connected, () => { stateMachine.Transition(APIState.Subscribing); }); stateMachine.AddHandler(APIState.Subscribing, () => { Subscribe(); }); stateMachine.AddHandler(APIState.Subscribed, () => { callback.Invoke(); }); stateMachine.Run(); }
private void ConnectedCallback(IAsyncResult asyncConnect) { if (!socket.Connected) { mConnectFailedCallback?.Invoke(); return; } mConnectCallback?.Invoke(); }
private void ConnectedCallback(IAsyncResult asyncConnect) { if (ServerSocket == null || !ServerSocket.Connected) { connectFailedDelegate?.Invoke(); return; } connectDelegate?.Invoke(); }
private void ConnectResult(object obj) { IAsyncResult result = (IAsyncResult)obj; //这里做一个超时的监测,当连接超过5秒还没成功表示超时 bool success = result.AsyncWaitHandle.WaitOne(5000, true); if (!success) { //超时 Closed(); connectFailedDelegate?.Invoke(); } else { //与socket建立连接成功,开启线程接受服务端数据。 isStopReceive = false; Client.Instance.Proxy.Socket = ServerSocket; Thread thread = new Thread(ReceiveSocket); thread.IsBackground = true; thread.Start(); } }
private void AwaitConnectResult() { mreConnect.WaitOne(); if (_terminalConnection != null) { _terminalControl = new TerminalControl(); _terminalSession = new TerminalSession(_terminalConnection, _terminalSettings); _terminalControl.Attach(_terminalSession); _connectCallback?.Invoke(this); } else { MessageBox.Show("Connection error : " + errorMsg, "Unable to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ProcessConnect(SocketAsyncEventArgs saea) { ConnectCallback?.Invoke(this, saea); }