Sia*_* Ys 10 javascript c# asp.net-mvc asp.net-mvc-4
基本上我正在我的网站上实现SignalR聊天.我已经可以向所有连接的用户发送消息,现在我希望添加"谁正在打字"功能.我正在尝试将其添加到$('#message').keypress
函数中并且它可以工作,但现在我无法向用户发送消息.
我做错了什么?
删除$('#message')后,keypress可以发送消息
没有删除$('#message').按键无法发送消息
我的HTML {
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" class="btn btn-default" />
<input type="hidden" id="displayname" />
<label id="isTyping" />
<ul id="discussion"></ul>
Run Code Online (Sandbox Code Playgroud)
}
以下是脚本:
<!--SignalR script to update the chat page and send messages.-->
<script type="text/javascript">
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages.
chat.client.broadcastMessage = function (name, message) {
$('#discussion').append('<li><strong>' + name
+ '</strong>: ' + message + '</li>');
};
chat.client.sayWhoIsTyping = function (name) {
$('#isTyping').html('<em>' + name + ' is typing...</em>');
setTimeout(function () {
$('#isTyping').html(' ');
}, 5000);
};
// 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.start().done(function () {
$('#sendmessage').click(function () {
var encodedName = $('<div />').text($('#displayname').val()).html();
var encodedMsg = $('<div />').text($('#message').val()).html();
chat.server.sendPublic(encodedName, encodedMsg);
$('#message').val('').focus();
});
$('#message').keypress(function (e) {
if (e.which == 13) {
var encodedName = $('<div />').text($('#displayname').val()).html();
var encodedMsg = $('<div />').text($('#message').val()).html();
chat.server.sendPublic(encodedName, encodedMsg);
$('#message').val('').focus();
} else {
var encodedName = $('<div />').text($('#displayname').val()).html();
chat.server.isTyping(encodedName);
}
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的Hub代码:
public void SendPublic(string name, string message)
{
// Call the addNewMessageToPage method to update clients
Clients.All.broadcastMessage(name, message);
}
public void IsTyping(string name)
{
SayWhoIsTyping(name);
}
public void SayWhoIsTyping(string name)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
context.Clients.All.sayWhoIsTyping(name);
}
Run Code Online (Sandbox Code Playgroud)
Luc*_*rei 10
在您的服务器上,您必须ChatHub
在名称中包含两个方法:
public void IsTyping (string html)
{
// do stuff with the html
SayWhoIsTyping(html); //call the function to send the html to the other clients
}
public void SayWhoIsTyping (string html)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
context.Clients.All.sayWhoIsTyping (html);
}
Run Code Online (Sandbox Code Playgroud)
这个过程如何运作?
您可以捕获您的某个客户正在键入的事件,即使用javascript的按键功能.然后,使用所需的html从服务器调用一个函数,其中包含名称和其他信息.服务器接收信息,处理它,然后从客户端调用一个函数,该函数显示谁正在键入.
在您的情况下,IsTyping
来自服务器的方法将处理来自客户端的信息,并且SayWhoIsTyping
来自服务器的方法将调用客户端来处理它所需的信息.
编辑你的评论:
我建议从这样的javascript修改函数:
chat.client.sayWhoIsTyping = function (name) {
$('#isTyping').html('<em>' + name + ' is typing...</em>');
setTimeout(function () {
$('#isTyping').html(' ');
}, 5000);
};
$('#message').keypress(function(e) {
if (e.which == 13) {
$('#sendmessage').trigger('click');
} else {
var encodedName = $('<div />').text($('#displayname').val()).html();
chat.server.isTyping(encodedName);
}
});
Run Code Online (Sandbox Code Playgroud)
并删除该
$(document).keypress
功能.
归档时间: |
|
查看次数: |
3245 次 |
最近记录: |