Last active
May 19, 2018 21:43
-
-
Save nbuss848/1f284ed1c8be52c149e80fae13d4f167 to your computer and use it in GitHub Desktop.
SignalR Simple 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Chat</title> | |
<script src="Scripts/jquery-3.0.0.min.js"></script> | |
<script src="Scripts/jquery.signalR-2.2.3.min.js"></script> | |
<script src="Scripts/bootstrap.min.js"></script> | |
<link href="Content/bootstrap.min.css" rel="stylesheet" /> | |
<script type="text/javascript"> | |
$(function () { | |
var connection = $.hubConnection(); | |
var hub = connection.createHubProxy("chatApp"); | |
hub.on("submitMessage", function (message, time, username) { | |
var myTextArea = $('#txChat'); | |
myTextArea.val(myTextArea.val() + '\n' + time + " - " + username + ": " + message); | |
}); | |
connection.start(function () { | |
// if you want to do something when connected to | |
// server maybe change a label to connected for example | |
}); | |
$("#btSubmit").click(function () { | |
var message = $("#txMessage").val(); | |
var username = $("#txUsername").val(); | |
hub.invoke("message", message, username.toUpperCase()); | |
$("#txMessage").val(""); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-sm-12 text-center"> | |
<h1>Chat</h1> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-sm-12 text-center"> | |
<textarea id="txChat" rows="20" cols="80"></textarea> | |
<br /> | |
<label class="label-control">Username</label> | |
<input id="txUsername" type="text" value="Bill" class="form-control" /> | |
<br /> | |
<label class="label-control">Message</label> | |
<input id="txMessage" type="text" class="form-control" /> | |
<br /> | |
<input id="btSubmit" type="button" value="Submit" class="btn btn-primary" /> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Microsoft.AspNet.SignalR; | |
using Microsoft.AspNet.SignalR.Hubs; | |
namespace SignalR | |
{ | |
[HubName("chatApp")] | |
public class ChatApp : Hub | |
{ | |
public void Message(string message, string username) | |
{ | |
var datetime = DateTime.Now; | |
Clients.All.submitMessage(message, datetime.ToShortDateString(), username); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment