wpf Signalr服务端如果配置给别的电脑客户端连接

JoJoTuna 2017-11-02 03:43:00
在网上找了个例子 的服务端的signalr 写完后 自己测试了连接成功的
private const string ServerUrl = "http://localhost:5566/"; 用的是这个地址
可是如果要和别人写的客户端测试 我改怎么让他能连接到我的服务端
webapi我知道可以发布到iis上 然后客户端访问地址就ok了
我用的是wpf写的 我不知道wpf要怎么配置这个
我看了 网上的 说可以App.config里:
<appSettings>
<!--通信连接地址-->
<add key="ServiceUri" value="http://192.168.18.103:46900/" />
</appSettings>
然后在: static string ServerURI = ConfigurationManager.AppSettings["ServiceUri"];
可是报错了


代码:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Configuration;

namespace WpfApp4
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{

public IDisposable SignalR { get; set; }
//private const string ServerUrl = "http://localhost:5566/";
//全局通信链接,地址为服务端所在主机IP地址,端口号可任意指定
static string ServerURI = ConfigurationManager.AppSettings["ServiceUri"];

public MainWindow()
{
InitializeComponent();
}

//启动SignalR服务,将SignalR服务寄宿在WPF程序中
private void StartServer()
{
try
{
SignalR = WebApp.Start(ServerURI);//启动SignalR服务
}
catch (TargetInvocationException e)
{
//WriteToConsole("一个服务已经运行在:" + ServerUrl);
WriteToConsole(e.Message);
return;
}
this.Dispatcher.Invoke(() => ButtonStop.IsEnabled = true);
WriteToConsole("服务已经成功启动,地址为:" + ServerUrl);
}


//将消息添加到消息列表中
public void WriteToConsole(String message)
{
if (!(TextBoxMessage.CheckAccess()))
{
this.Dispatcher.Invoke(() => WriteToConsole(message));
return;
}
TextBoxMessage.AppendText(message + "\r");
}

private void ButtonStart_Click(object sender, RoutedEventArgs e)
{
WriteToConsole("正在启动服务...");//消息列表
Task.Run(() => StartServer()); //异步启动SignalR服务
}

private void ButtonStop_Click(object sender, RoutedEventArgs e)
{
SignalR.Dispose();//释放资源
Close();
}
}

public class ChatHub : Hub
{
public void Send()
{
Clients.All.SendMessaeg();
}

public override Task OnConnected()
{


Application.Current.Dispatcher.Invoke(() => ((MainWindow)Application.Current.MainWindow).WriteToConsole("客户端连接成功,连接ID是:" + Context.ConnectionId));

return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
Application.Current.Dispatcher.Invoke(() => ((MainWindow)Application.Current.MainWindow).WriteToConsole("客户端断开连接,连接ID是:" + Context.ConnectionId));
return base.OnDisconnected(true);
}
}
}
...全文
133 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧