signalr2 asp.net ,vc5 如何为发送和接收的消息设置不同的 css 样式?
signalr2 asp.net ,vc5 how to set different css styles for messages that sent and received?
我使用 ASP.NET MVC 5 和 SignalR 2。
我在一个视图中有一个聊天页面,
和另一个局部视图中的一个聊天页面,
我对发送的消息和接收的消息有不同的风格。
如何为发送和接收的消息设置此样式?
所有的网络信件都是一个聊天view.so没有不同的风格。
谢谢:)
@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(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.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// 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 () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
您需要一个标志,您可以根据该标志区分来自服务器和发往服务器的消息。你还需要一些 css 来标记不同的消息。
样本:
chat.client.addNewMessageToPage = function (name, message, fromServer) {
// Add the message to the page.
if(fromServer){
$('#discussion').append('<li class="messageFromServer">strong>'+htmlEncode(name)+'</strong>: '+htmlEncode(message) + '</li>');
}
else{
$('#discussion').append('<li class="messageToServer"><strong>'+htmlEncode(name)+'</strong>: '+htmlEncode(message)+'</li>');
}
};
样本css:
.messageFromServer{
color:purple
}
.messageToServer{
color:red
(你可以在 fiddler 上测试 css 东西:
https://jsfiddle.net/ugrf0jea/7/)
您的中心:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Mark the message, that this is from other client. Only call methode "addNewMessageToPage" with this params on other clients(not on caller)
Clients.Others.addNewMessageToPage(name, message, false);
// Mark message that this is from current client. Only call method addNewMessageToPage" on caller cliebt
Clients.Caller.addNewMessageToPage(name, message, false);
}
}
我使用 ASP.NET MVC 5 和 SignalR 2。 我在一个视图中有一个聊天页面, 和另一个局部视图中的一个聊天页面, 我对发送的消息和接收的消息有不同的风格。 如何为发送和接收的消息设置此样式?
所有的网络信件都是一个聊天view.so没有不同的风格。
谢谢:)
@{
ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(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.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// 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 () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
您需要一个标志,您可以根据该标志区分来自服务器和发往服务器的消息。你还需要一些 css 来标记不同的消息。
样本:
chat.client.addNewMessageToPage = function (name, message, fromServer) {
// Add the message to the page.
if(fromServer){
$('#discussion').append('<li class="messageFromServer">strong>'+htmlEncode(name)+'</strong>: '+htmlEncode(message) + '</li>');
}
else{
$('#discussion').append('<li class="messageToServer"><strong>'+htmlEncode(name)+'</strong>: '+htmlEncode(message)+'</li>');
}
};
样本css:
.messageFromServer{
color:purple
}
.messageToServer{
color:red
(你可以在 fiddler 上测试 css 东西: https://jsfiddle.net/ugrf0jea/7/)
您的中心:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Mark the message, that this is from other client. Only call methode "addNewMessageToPage" with this params on other clients(not on caller)
Clients.Others.addNewMessageToPage(name, message, false);
// Mark message that this is from current client. Only call method addNewMessageToPage" on caller cliebt
Clients.Caller.addNewMessageToPage(name, message, false);
}
}