<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" />
<input type="text" id="reId" />
<input type="button" id="sendmessage" value="Send" />
<input type="button" id="SendImage" value="SendImage" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.8.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.1.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://192.168.8.163:2323/signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://192.168.8.163:2323/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.myHub;
// Create a function that the hub can call to broadcast messages.
chat.client.PushMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Create a function that the hub can call to broadcast messages.
chat.client.receiveImage = function (name,url) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').html("<img src='"+url+"' />").html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
//function connectionStateChanged(state) {
// if(state.newState==4)
// {
// setTimeout(function(){
// $('#discussion').append('<li><strong>正在重连</strong>: </li>');
// $.connection.hub.start();
// },2000);
// }
// var stateConversion = {0: 'connecting', 1: 'connected', 2: 'reconnecting', 4: 'disconnected'};
// console.log('SignalR state changed from: ' + stateConversion[state.oldState]
// + ' to: ' + stateConversion[state.newState]);
//}
//$.connection.hub.stateChanged(connectionStateChanged);
$.connection.hub.disconnected(function(){
setTimeout(function () {
$.connection.hub.start();
},2000);
});
//断线重连 没2000ms执行一次
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.qs={"userId":"111111"};
$.connection.hub.start({ waitForPageLoad: false }).done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
//chat.server.send($('#reId').val(),$('#displayname').val(), $('#message').val());
chat.server.SendMessage($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
$('#SendImage').click(function () {
// Call the Send method on the hub.
//chat.server.send($('#reId').val(),$('#displayname').val(), $('#message').val());
chat.server.Monitor($('#displayname').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>