SignalR .Net 客户:如何向组发送消息?
我使用了一个对话应用程序的示例 SignalR Wiki 入门页面集线器。 我扩展了它以添加组支持,它工作正常。
但是,现在我想从外部控制台应用程序向组发送消息。 以下是Console应用程序的代码,以及用于组的代码下方。 如何从代理服务器向组发送消息? 是否有可能?
SignalR Web应用程序主机:
Default.aspx
但是,现在我想从外部控制台应用程序向组发送消息。 以下是Console应用程序的代码,以及用于组的代码下方。 如何从代理服务器向组发送消息? 是否有可能?
// Console App
using System;
using Microsoft.AspNet.SignalR.Client.Hubs;
namespace SignalrNetClient
{
class Program
{
static void Main/string[] args/
{
// Connect to the service
var connection = new HubConnection/"[url=http://localhost:50116"/;]http://localhost:50116"/;[/url]
var chatHub = connection.CreateHubProxy/"Chat"/;
// Print the message when it comes in
connection.Received += data => Console.WriteLine/data/;
// Start the connection
connection.Start//.Wait//;
chatHub.Invoke/"Send", "Hey there!"/;
string line = null;
while //line = Console.ReadLine/// != null/
{
// Send a message to the server
connection.Send/line/.Wait//;
}
}
}
}
SignalR Web应用程序主机:
namespace SignalrServer.Hubs
{
public class Chat : Hub
{
public void Send/string message/
{
// Call the addMessage method on all clients
Clients.All.addMessage/message/;
Clients.Group/"RoomA"/.addMessage/"Group Message " + message/;
}
//server
public void Join/string groupName/
{
Groups.Add/Context.ConnectionId, groupName/;
}
}
}
Default.aspx
<script src="[url=http://code.jquery.com/jquery-1.8.2.min.js"]http://code.jquery.com/jquery-1.8.2.min.js"[/url] type="text/javascript"></script>
<script src="Scripts/jquery.signalR-1.0.1.min.js" type="text/javascript"></script>
If this is an MVC project then use the following
<script src="~/signalr/hubs" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$/function // {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function /message/ {
$/'#messages'/.append/'<li>' + message + '</li>'/;
};
$.connection.hub.start/function // {
chat.server.join/"RoomA"/;
}/;
// Start the connection
$.connection.hub.start//.done/function // {
$/"#broadcast"/.click/function // {
// Call the chat method on the server
chat.server.send/$/'#msg'/.val///;
}/;
}/;
}/;
</script>
<div>
<input id="msg" type="text"/>
<input id="broadcast" type="button" value="broadcast"/>
<ul id="messages">
</ul>
</div>
没有找到相关结果
已邀请:
2 个回复
二哥
赞同来自:
你的新课堂
然后在接受此对象的集线器中创建一种方法。
然后您可以从客户端传输此对象。
然后您也可以从客户端调用它 JS
</mymessage>
二哥
赞同来自:
该小组不再有效。
服务器代码
:
客户端代码
:
它不会从客户端上的服务器收到任何消息。
可以发送消息。
</string></string>