मैं सिग्नलआर विकी प्रारंभिक हब्स पेज से नमूना चैट एप्लिकेशन का उपयोग कर रहा हूं। मैंने इसे समूह समर्थन जोड़ने के लिए बढ़ा दिया है और यह ठीक काम कर रहा है।सिग्नलआर नेट क्लाइंट: मैं समूह को संदेश कैसे भेजूं?
हालांकि, अब मैं बाहरी कंसोल एप्लिकेशन से समूह को एक संदेश भेजना चाहता हूं। कंसोल ऐप के लिए मेरा कोड और नीचे समूह के लिए मेरा कोड है। मैं प्रॉक्सी से समूह को संदेश कैसे भेजूं? क्या यह संभव है?
// 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("http://localhost:50116");
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 वेब एप्लिकेशन होस्ट:
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="http://code.jquery.com/jquery-1.8.2.min.js" 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 type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages">
</ul>
</div>
मैं इसे ले आप नाम पता संदेश भेजने से पहले समूह का? –
हां, मेरे पास समूह का नाम है। – robrtc
अभी के लिए मैं समूह के नाम को संदेश के साथ जोड़ता हूं और फिर भेजें विधि के अंदर स्ट्रिंग को विभाजित करता हूं। हालांकि, अगर मैं इसके लिए एक और अधिक सुरुचिपूर्ण समाधान है तो मैं उत्सुक हूं। – robrtc