2017-04-17 107 views
0

我與signalR.I工作有一個問題。我不在javascipt客戶端中創建查詢字符串。我該怎麼辦?signalR javascript客戶端查詢字符串

Javascript代碼

$.connection.hub.url = "http://localhost:8090/signalr"; 

var chat = $.connection.myHub2; 
// $.connection.hub.qs = { 'useridbrowser': 'session1' }; 
$.connection.hub.qs = "useridbrowser= session1"; 
$.connection.hub.start({ 'useridbrowser': 'session1' 
}).done(function() { 
} 

Server代碼

public override Task OnConnected() 
{    
    mouseqs = Context.QueryString["useridbrowser"]; 
    return (base.OnConnected()); 
} 

我寫

$.connection.hub.qs = { 'useridbrowser': 'session1' } 

但不工作

回答

0

請參考下面的javascript:

<script src="Scripts/jquery-1.6.4.min.js" ></script> 
<script src="Scripts/jquery.signalR-1.1.4.min.js"></script> 
<script src="signalr/hubs"></script> 
<script type="text/javascript"> 
    $(function() { 
     var chat = $.connection.CallCenterHub; 
     $.connection.hub.qs = { 'ClientType': 'WebUser', 'UserId': $('#txtUserId').val() }; 

     chat.client.onInstantMessage = function (MessageId, MessageFrom, MessageTo, MessageText, MessageDateTime) { 

      if (MessageFrom != $('#txtUserId').val()) { 
       $('#txtConnectedUser').val(MessageFrom); 
      } 


      if (MessageFrom == $('#txtUserId').val()) { 
       $('#MessagesArea').append('<div class="well" style = "height:100%;padding-top:1px;padding-left:1px;padding-right:1px;padding-bottom:1px;width:100%;"><table style="width: 100%; table-layout: fixed;"><tr><td width="32px" valign="top"><img src="Images/IM32.png" height="32" width="32"></td><td><table style="width: 100%; table-layout: fixed;"><tr><td><table style="width: 100%; table-layout: fixed;"><tr><td width="50%"><b>&nbsp;&nbsp;Me:</b></td><td align="right" width="50%">' + MessageDateTime + '</td></tr></table></td></tr><tr><td style = "color: #000000;white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap:break-word;">&nbsp;&nbsp;' + MessageText + '</td></tr></table></td></tr></table></div>'); 
      } 
      else { 
       $('#MessagesArea').append('<div class="well" style = "height:100%;padding-top:1px;padding-left:1px;padding-right:1px;padding-bottom:1px;width:100%;"><table style="width: 100%; table-layout: fixed;"><tr><td width="32px" valign="top"><img src="Images/IM2_32.png" height="32" width="32"></td><td><table style="width: 100%; table-layout: fixed;"><tr><td><table style="width: 100%; table-layout: fixed;"><tr><td width="50%"><b>&nbsp;&nbsp;' + MessageFrom + ':</b></td><td align="right" width="50%">' + MessageDateTime + '</td></tr></table></td></tr><tr><td style = "color: #000000;white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap:break-word;">&nbsp;&nbsp;' + MessageText + '</td></tr></table></td></tr></table></div>'); 
      } 

      var objDiv = document.getElementById("MessagesAreaAll"); 
      objDiv.scrollTop = objDiv.scrollHeight; 

     }; 

     $('#message').focus(); 
     $.connection.hub.start().done(function() { 
      $('#sendmessage').click(function() { 
       chat.server.sendInstantMessage($('#txtUserId').val(), $('#txtConnectedUser').val(), $('#message').val()); 
       $('#message').val('').focus(); 
      }); 
     }); 

     $('#message').keydown(function (event) { 
      if (event.keyCode == 13) { 
       chat.server.sendInstantMessage($('#txtUserId').val(), $('#txtConnectedUser').val(), $('#message').val()); 
       $('#message').val('').focus(); 
      } 
     }); 

    }); 
</script>