【发布时间】:2014-11-10 10:32:21
【问题描述】:
我已经制作了一个控制台应用程序,它作为信号 r 的主机并监听端口 8089。当我运行 wpf 应用程序并在 localhost 上运行该主机应用程序时,一切正常。现在我已经在服务器上部署了该控制台应用程序.现在当我尝试连接时,它没有通过 wpf 连接。所以后来我读到它只支持 HTTP 协议,读完之后我做了一个网站,它连接到远程服务器上的服务器,并且连接成功。我的问题是有什么方法可以使用 wpf 连接到该服务器。我想制作一个桌面聊天应用程序,其中所有客户端都将连接到同一服务器以便它可以广播。任何帮助将不胜感激
StartUp.CS
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
hubConfiguration.EnableDetailedErrors = true;
map.RunSignalR(hubConfiguration);
});
}
}
**Program.CS**
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://MyIpAddressOfServer:8089";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
**MainWindowXaml.cs**
public partial class MainWindow : Window
{
public System.Threading.Thread Thread { get; set; }
public string Host = "http://MyIpAddressOfServer:8089/signalr";
public IHubProxy Proxy { get; set; }
public HubConnection Connection { get; set; }
public bool Active { get; set; }
public MainWindow()
{
InitializeComponent();
}
private async void ActionSendButtonClick(object sender, RoutedEventArgs e)
{
await SendMessage();
}
private async Task SendMessage()
{
await Proxy.Invoke("Addmessage", ClientNameTextBox.Text , MessageTextBox.Text);
}
private async void ActionWindowLoaded(object sender, RoutedEventArgs e)
{
Active = true;
Thread = new System.Threading.Thread(() =>
{
Connection = new HubConnection(Host);
Proxy = Connection.CreateHubProxy("MyHub");
Proxy.On<string, string>("addmessage", (name, message) => OnSendData(name + ": " + message ));
Connection.Start();
while (Active)
{
System.Threading.Thread.Sleep(10);
}
}) { IsBackground = true };
Thread.Start();
}
private void OnSendData(string message)
{
try
{
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => MessagesListBox.Items.Insert(0, message)));
}
catch (Exception e)
{
}
}
private async void ActionMessageTextBoxOnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter || e.Key == Key.Return)
{
await SendMessage();
MessageTextBox.Text = "";
}
}
}
【问题讨论】:
标签: .net asp.net-mvc deployment signalr signalr-hub