Пример #1
0
        public void Send(byte[] data)
        {
            if (socketToken.socketState != SocketState.OPENED)
            {
                WebuSocketErrorEnum ev    = WebuSocketErrorEnum.UNKNOWN_ERROR;
                Exception           error = null;
                switch (socketToken.socketState)
                {
                case SocketState.OPENING: {
                    ev    = WebuSocketErrorEnum.CONNECTING;
                    error = new Exception("send error:" + "not yet connected.");
                    break;
                }

                case SocketState.CLOSING:
                case SocketState.CLOSED: {
                    ev    = WebuSocketErrorEnum.ALREADY_DISCONNECTED;
                    error = new Exception("send error:" + "connection was already closed. please create new connection by new WebuSocket().");
                    break;
                }
                }
                if (OnError != null)
                {
                    OnError(ev, error);
                }
                return;
            }

            var payloadBytes = WebSocketByteGenerator.SendBinaryData(data);

            socketToken.sendArgs.SetBuffer(payloadBytes, 0, payloadBytes.Length);
            if (!socketToken.socket.SendAsync(socketToken.sendArgs))
            {
                OnSend(socketToken.socket, socketToken.sendArgs);
            }
        }
Пример #2
0
        public void Send(byte[] data)
        {
            if (socketToken.socketState != SocketState.OPENED)
            {
                WebuSocketErrorEnum ev    = WebuSocketErrorEnum.UNKNOWN_ERROR;
                Exception           error = null;
                switch (socketToken.socketState)
                {
                case SocketState.TLS_HANDSHAKING:
                case SocketState.WS_HANDSHAKING:
                {
                    ev    = WebuSocketErrorEnum.CONNECTING;
                    error = new Exception("send error:" + "not yet connected.");
                    break;
                }

                case SocketState.CLOSING:
                case SocketState.CLOSED:
                {
                    ev    = WebuSocketErrorEnum.ALREADY_DISCONNECTED;
                    error = new Exception("send error:" + "connection was already closed. please create new connection by new WebuSocket().");
                    break;
                }

                default:
                {
                    ev    = WebuSocketErrorEnum.CONNECTING;
                    error = new Exception("send error:" + "not yet connected.");
                    break;
                }
                }
                // if (OnError != null) OnError(ev, error);
                return;
            }

            var payloadBytes = WebSocketByteGenerator.SendBinaryData(data, false);

            {
                try
                {
                    socketToken.socket.BeginSend(
                        payloadBytes,
                        0,
                        payloadBytes.Length,
                        SocketFlags.None,
                        result =>
                    {
                        var s   = (Socket)result.AsyncState;
                        var len = s.EndSend(result);
                        if (0 < len)
                        {
                            // do nothing.
                        }
                        else
                        {
                            Debug.LogError("send error:" + "send failed by unknown reason.");
                            // // send failed.
                            // if (OnError != null)
                            // {
                            //     var error = new Exception("send error:" + "send failed by unknown reason.");
                            //     OnError(WebuSocketErrorEnum.SEND_FAILED, error);
                            // }
                        }
                    },
                        socketToken.socket
                        );
                }
                catch (Exception e)
                {
                    // disconnectをどう行うか。失敗をどうハンドルするか、今後考える必要がある。
                    // if (OnError != null)
                    // {
                    //     OnError(WebuSocketErrorEnum.SEND_FAILED, e);
                    // }
                    // Disconnect();
                }
            }
        }