This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a242071
commit e2c31ec
Showing
6 changed files
with
128 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 121 additions & 121 deletions
242
Pusher.Connections.WindowsStore/WebSocket.cs → ...tions.WindowsStore/WebSocketConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,122 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Pusher.Events; | ||
using Windows.Networking.Sockets; | ||
using Windows.Storage.Streams; | ||
|
||
namespace Pusher.Connections.WindowsStore | ||
{ | ||
public class WebSocket : IConnection, IDisposable | ||
{ | ||
private MessageWebSocket _socket; | ||
private DataWriter _messageWriter; | ||
private readonly Uri _endpoint; | ||
private ConnectionState _connectionState; | ||
|
||
public WebSocket(Uri endpoint) | ||
{ | ||
_endpoint = endpoint; | ||
SetupSocket(); | ||
} | ||
|
||
private void SetupSocket() | ||
{ | ||
_socket = new MessageWebSocket(); | ||
_socket.Control.MessageType = SocketMessageType.Utf8; | ||
_socket.Closed += OnSocketClosed; | ||
_socket.MessageReceived += OnMessageReceived; | ||
} | ||
|
||
private async void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) | ||
{ | ||
if (OnData == null) return; | ||
|
||
var exceptionOccured = false; | ||
try | ||
{ | ||
var reader = args.GetDataReader(); | ||
var text = reader.ReadString(reader.UnconsumedBufferLength); | ||
OnData(sender, new DataReceivedEventArgs { TextData = text }); | ||
} | ||
catch (Exception) | ||
{ | ||
exceptionOccured = true; | ||
} | ||
// cannot await in catch | ||
if (exceptionOccured) await Reconnect(); | ||
} | ||
|
||
private async Task Reconnect() | ||
{ | ||
if (_connectionState == ConnectionState.Connecting || _connectionState == ConnectionState.Connected) return; | ||
|
||
SetupSocket(); | ||
await Open(); | ||
} | ||
|
||
private async void OnSocketClosed(IWebSocket sender, WebSocketClosedEventArgs args) | ||
{ | ||
_messageWriter = null; | ||
if (_connectionState != ConnectionState.Disconnecting) | ||
{ | ||
await Reconnect(); | ||
} | ||
_connectionState = ConnectionState.Disconnected; | ||
if (OnClose != null) OnClose(sender, new EventArgs()); | ||
} | ||
|
||
#region Implementation of IConnection | ||
|
||
public void Close() | ||
{ | ||
_connectionState = ConnectionState.Disconnecting; | ||
_socket.Close(1000, "Close requested"); | ||
_connectionState = ConnectionState.Disconnected; | ||
} | ||
|
||
public async Task Open() | ||
{ | ||
if (_connectionState == ConnectionState.Connected) | ||
{ | ||
Close(); | ||
SetupSocket(); | ||
} | ||
_connectionState = ConnectionState.Connecting; | ||
await _socket.ConnectAsync(_endpoint); | ||
_messageWriter = new DataWriter(_socket.OutputStream); | ||
_connectionState = ConnectionState.Connected; | ||
if (OnOpen != null) | ||
{ | ||
OnOpen(this, new EventArgs()); | ||
} | ||
} | ||
|
||
public async Task SendMessage(string data) | ||
{ | ||
if (_messageWriter == null) | ||
{ | ||
await Open(); | ||
} | ||
Debug.Assert(_messageWriter != null); | ||
|
||
_messageWriter.WriteString(data); | ||
await _messageWriter.StoreAsync(); | ||
} | ||
|
||
public event EventHandler<EventArgs> OnClose; | ||
public event EventHandler<EventArgs> OnOpen; | ||
public event EventHandler<DataReceivedEventArgs> OnData; | ||
|
||
#endregion | ||
|
||
#region Implementation of IDisposable | ||
|
||
public void Dispose() | ||
{ | ||
_socket.Dispose(); | ||
} | ||
|
||
#endregion | ||
} | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Pusher.Events; | ||
using Windows.Networking.Sockets; | ||
using Windows.Storage.Streams; | ||
|
||
namespace Pusher.Connections.WindowsStore | ||
{ | ||
public class WebSocketConnection : IConnection, IDisposable | ||
{ | ||
private MessageWebSocket _socket; | ||
private DataWriter _messageWriter; | ||
private readonly Uri _endpoint; | ||
private ConnectionState _connectionState; | ||
|
||
public WebSocketConnection(Uri endpoint) | ||
{ | ||
_endpoint = endpoint; | ||
SetupSocket(); | ||
} | ||
|
||
private void SetupSocket() | ||
{ | ||
_socket = new MessageWebSocket(); | ||
_socket.Control.MessageType = SocketMessageType.Utf8; | ||
_socket.Closed += OnSocketClosed; | ||
_socket.MessageReceived += OnMessageReceived; | ||
} | ||
|
||
private async void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) | ||
{ | ||
if (OnData == null) return; | ||
|
||
var exceptionOccured = false; | ||
try | ||
{ | ||
var reader = args.GetDataReader(); | ||
var text = reader.ReadString(reader.UnconsumedBufferLength); | ||
OnData(sender, new DataReceivedEventArgs { TextData = text }); | ||
} | ||
catch (Exception) | ||
{ | ||
exceptionOccured = true; | ||
} | ||
// cannot await in catch | ||
if (exceptionOccured) await Reconnect(); | ||
} | ||
|
||
private async Task Reconnect() | ||
{ | ||
if (_connectionState == ConnectionState.Connecting || _connectionState == ConnectionState.Connected) return; | ||
|
||
SetupSocket(); | ||
await Open(); | ||
} | ||
|
||
private async void OnSocketClosed(IWebSocket sender, WebSocketClosedEventArgs args) | ||
{ | ||
_messageWriter = null; | ||
if (_connectionState != ConnectionState.Disconnecting) | ||
{ | ||
await Reconnect(); | ||
} | ||
_connectionState = ConnectionState.Disconnected; | ||
if (OnClose != null) OnClose(sender, new EventArgs()); | ||
} | ||
|
||
#region Implementation of IConnection | ||
|
||
public void Close() | ||
{ | ||
_connectionState = ConnectionState.Disconnecting; | ||
_socket.Close(1000, "Close requested"); | ||
_connectionState = ConnectionState.Disconnected; | ||
} | ||
|
||
public async Task Open() | ||
{ | ||
if (_connectionState == ConnectionState.Connected) | ||
{ | ||
Close(); | ||
SetupSocket(); | ||
} | ||
_connectionState = ConnectionState.Connecting; | ||
await _socket.ConnectAsync(_endpoint); | ||
_messageWriter = new DataWriter(_socket.OutputStream); | ||
_connectionState = ConnectionState.Connected; | ||
if (OnOpen != null) | ||
{ | ||
OnOpen(this, new EventArgs()); | ||
} | ||
} | ||
|
||
public async Task SendMessage(string data) | ||
{ | ||
if (_messageWriter == null) | ||
{ | ||
await Open(); | ||
} | ||
Debug.Assert(_messageWriter != null); | ||
|
||
_messageWriter.WriteString(data); | ||
await _messageWriter.StoreAsync(); | ||
} | ||
|
||
public event EventHandler<EventArgs> OnClose; | ||
public event EventHandler<EventArgs> OnOpen; | ||
public event EventHandler<DataReceivedEventArgs> OnData; | ||
|
||
#endregion | ||
|
||
#region Implementation of IDisposable | ||
|
||
public void Dispose() | ||
{ | ||
_socket.Dispose(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters