Skip to content

Instantly share code, notes, and snippets.

@ichensky
Created October 27, 2017 12:07
Show Gist options
  • Save ichensky/3b8c23e7d0d55140e88bb27d966ce186 to your computer and use it in GitHub Desktop.
Save ichensky/3b8c23e7d0d55140e88bb27d966ce186 to your computer and use it in GitHub Desktop.
https://weblogs.asp.net/ricardoperes/signalr-in-asp-net-core
public class ChatHub : Hub
{
public async Task Send(string message)
{
//await this.Clients.All.InvokeAsync("Send", message);
await this.Clients.Client(this.Context.ConnectionId).InvokeAsync(nameof(Send), message);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<script src="lib/signalr-client-1.0.0-alpha2-final.min.js"></script>
</head>
<body>
<script>
var transportType = signalR.TransportType.WebSockets;
var chatHub = new signalR.HttpConnection(`http://${document.location.host}/chat`, { transport: transportType });
var connection = new signalR.HubConnection(chatHub);
function exec(method, chatHub, connection,invoke) {
if (!method(chatHub, connection)) {
setTimeout(function () {
exec(method, chatHub, connection, invoke);
}, 50);
} else {
invoke();
}
}
function startConnection(chatHub, connection) {
if (chatHub.connectionState == 0 || chatHub.connectionState == 3) {
connection.start();
return false;
}
if (chatHub.connectionState != 2) {
return false;
}
return true;
}
function post(chatHub, connection, method, message) {
exec(startConnection, chatHub, connection, function () {
connection.invoke('Send', message);
});
}
connection.on('Send', (message) => {
console.log('received message:' + message);
});
post(chatHub, connection, 'Send', "bla-bla-bla");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment