Created
August 22, 2012 15:06
-
-
Save bradygaster-zz/3426554 to your computer and use it in GitHub Desktop.
SignalR 0.5.3 Sample Code
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
[HubName("chat")] | |
public class ChatHub : Hub | |
{ | |
public void SendMessage(dynamic message) | |
{ | |
Clients.receiveMessage(message); | |
} | |
} |
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
private static void Connect() | |
{ | |
var connection = new HubConnection("http://localhost:58416/"); | |
var chat = connection.CreateProxy("chat"); | |
chat.On<string>("addMessage", Console.WriteLine); | |
try | |
{ | |
connection.Start().Wait(); | |
string msg = null; | |
while ((msg = Console.ReadLine()) != null) | |
{ | |
chat.Invoke("send", msg).Wait(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
using (var error = ex.GetError()) // NEW ERROR HANDLING FEATURES | |
{ | |
Console.WriteLine(error.StatusCode); | |
} | |
} | |
} |
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
var chat = connection.createProxy('chat'); |
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
var connection = $.hubConnection('http://mysite.azurewebsites.net'); |
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
public void DynamicInvoke(string method) | |
{ | |
IClientProxy proxy = Caller; | |
IClientProxy clientsProxy = Clients; | |
IClientProxy groupProxy = Clients["foo"]; | |
proxy.Invoke(method); | |
} |
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
connection.logging = true; |
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
var connection = $.hubConnection(); | |
connection.start(function () { | |
$('#submitButton').removeAttr('disabled'); | |
}); | |
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
$(function () { | |
var connection = $.hubConnection(); | |
connection.start(function () { | |
$('#submitButton').removeAttr('disabled'); | |
}); | |
var chat = connection.createProxy('chat'); | |
$('#submitButton').click(function () { | |
var msg = { | |
username: $('#username').val(), | |
message: $('#message').val() | |
}; | |
chat.invoke('SendMessage', msg); | |
}); | |
}); |
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
chat.on('receiveMessage', function (m) { | |
$('#messages').append('<li><b>' + m.username + '</b>:' + m.message + '</li>'); | |
}); |
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
public void SendMessage(dynamic message) | |
{ | |
var user = message.username; | |
if (String.IsNullOrEmpty(message.username.ToString())) | |
{ | |
throw new ApplicationException("No username provided!"); | |
} | |
Clients.receiveMessage(message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment