-
Notifications
You must be signed in to change notification settings - Fork 19
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
Showing
6 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Microsoft.AspNetCore.Sockets; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SignalRCore.Web.EndPoints | ||
{ | ||
public class MessagesEndPoint : EndPoint | ||
{ | ||
public ConnectionList Connections { get; } = new ConnectionList(); | ||
|
||
public override async Task OnConnectedAsync(Connection connection) | ||
{ | ||
Connections.Add(connection); | ||
await Broadcast($"{connection.ConnectionId} connected ({connection.Metadata[ConnectionMetadataNames.Transport]})"); | ||
|
||
try | ||
{ | ||
while (await connection.Transport.Input.WaitToReadAsync()) | ||
{ | ||
Message message; | ||
if (connection.Transport.Input.TryRead(out message)) | ||
{ | ||
var text = Encoding.UTF8.GetString(message.Payload); | ||
text = $"{connection.ConnectionId}: {text}"; | ||
await Broadcast(Encoding.UTF8.GetBytes(text), message.Type, message.EndOfMessage); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
Connections.Remove(connection); | ||
await Broadcast($"{connection.ConnectionId} disconnected ({connection.Metadata[ConnectionMetadataNames.Transport]})"); | ||
} | ||
} | ||
|
||
private Task Broadcast(string text) | ||
{ | ||
return Broadcast(Encoding.UTF8.GetBytes(text), MessageType.Text, endOfMessage: true); | ||
} | ||
|
||
private Task Broadcast(byte[] payload, MessageType format, bool endOfMessage) | ||
{ | ||
var tasks = new List<Task>(Connections.Count); | ||
foreach (var c in Connections) | ||
{ | ||
tasks.Add(c.Transport.Output.WriteAsync(new Message( | ||
payload, | ||
format, | ||
endOfMessage))); | ||
} | ||
|
||
return Task.WhenAll(tasks); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title></title> | ||
</head> | ||
<body> | ||
<h1 id="transportName">Websocket Transport</h1> | ||
<form id="sendmessage"> | ||
<input type="text" id="data" /> | ||
<input type="submit" value="Send" /> | ||
</form> | ||
<ul id="messages"></ul> | ||
<script src="lib/signalr/signalr-client.js"></script> | ||
<script> | ||
document.addEventListener('DOMContentLoaded', () => { | ||
let url = `http://${document.location.host}/message` | ||
let connection = new signalR.Connection(url); | ||
|
||
connection.onDataReceived = data => { | ||
let child = document.createElement('li'); | ||
child.innerText = data; | ||
document.getElementById('messages').appendChild(child); | ||
}; | ||
|
||
document.getElementById('sendmessage').addEventListener('submit', event => { | ||
let data = document.getElementById('data').value; | ||
connection.send(data); | ||
event.preventDefault(); | ||
}); | ||
|
||
connection.start(signalR.TransportType.WebSockets).then(() => { | ||
console.log("Opened"); | ||
}, () => { | ||
console.log("Error opening connection"); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |