【发布时间】:2018-03-25 15:06:09
【问题描述】:
我已经构建了名为“MyHub”的 Windows 窗体应用程序信号服务器和集线器。我创建了新的 MVC 项目网站来连接到我的 Windows 窗体服务器,但是我与如何连接到服务器堆叠在一起。你能帮我吗?这是我下面的当前代码。除了在“MyHub”上,目前还有一种名为“Send”的方法向所有人发送消息,如何制作类似:HubProxy.On(来自 win 表单)但在 mvc 中。
@{
ViewBag.Title = "Chat";
}
<fieldset>
<legend style="color:orangered">Welcome To Satya's signalR MVC Group Chat Club</legend>
</fieldset>
<div class="form-group col-xl-12">
<label style="color: blue; font-style: oblique;font-size: medium" id="label1">Write Your Message Here!</label><br />
<textarea class="form-control" rows="4" cols="40" id="message" placeholder="Share what's in your mind..."></textarea>
<br />
<input type="button" class="btn btn-primary" id="sendmessage" value="Send" />
<br />
<br />
<label style="color: blue;font-style:oblique;font-size:medium" id="label2">Group Chat Conversations History</label>
<div class="container chatArea">
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
</div>
@section scripts {
<script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
var chat = "MyHub"
chat.hub.url = 'http://xxxxxxxx:4848/';
chat.hub.qs = { 'username' : 'John' };
chat.client.Send = function (name, message) {
$('#discussion').append('<ul style="list-style-type:square"><li><strong style="color:red;font-style:normal;font-size:medium;text-transform:uppercase">' + htmlEncode(name) + ' ' + '<strong style="color:black;font-style:normal;font-size:medium;text-transform:lowercase">said</strong>'
+ '</strong>: ' + '<strong style="color:blue;font-style:oblique;font-size:medium">' + htmlEncode(message) + '</strong>' + '</li></ul>');
};
$('#displayname').val(prompt('Your Good Name Please:', ''));
$('#message').focus();
chat.start().done(function () {
$('#sendmessage').click(function () {
chat.client.Send($('#displayname').val(), $('#message').val());
$('#message').val('').focus();
});
});
});
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
编辑:
在服务器端:
class Startup
{
public void Configuration(IAppBuilder app)
{
//app.UseCors(CorsOptions.AllowAll);
//app.MapSignalR();
// 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);
});
}
}
public class MyHub : Hub
{
public async Task Send(string name, string message)
{
await Clients.All.addMessage(name, message);
uListHistory.Add(new ChatHistory { name = name, message = message });
}
....
现在在 MVC 客户端我有:
@{
ViewBag.Title = "Chat";
}
<fieldset>
<legend style="color:orangered">Welcome To Satya's signalR MVC Group Chat Club</legend>
</fieldset>
<div class="form-group col-xl-12">
<label style="color: blue; font-style: oblique;font-size: medium" id="label1">Write Your Message Here!</label><br />
<textarea class="form-control" rows="4" cols="40" id="message" placeholder="Share what's in your mind..."></textarea>
<br />
<input type="button" class="btn btn-primary" id="sendmessage" value="Send" />
<br />
<br />
<label style="color: blue;font-style:oblique;font-size:medium" id="label2">Group Chat Conversations History</label>
<div class="container chatArea">
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
</div>
@section scripts {
<script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
<script src="http://52.236.38.106:4848/signalr/hubs"></script>
<script>
$(function () {
$.connection.hub.url = "http://52.236.38.106:4848/signalr";
var chat = $.connection.myHub;
chat.client.AddMessage = function (name, message) {
$('#discussion').append('<ul style="list-style-type:square"><li><strong style="color:red;font-style:normal;font-size:medium;text-transform:uppercase">' + htmlEncode(name) + ' ' + '<strong style="color:black;font-style:normal;font-size:medium;text-transform:lowercase">said</strong>'
+ '</strong>: ' + '<strong style="color:blue;font-style:oblique;font-size:medium">' + htmlEncode(message) + '</strong>' + '</li></ul>');
};
$('#displayname').val(prompt('Your Good Name Please:', ''));
$('#message').focus();
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
chat.server.send($('#displayname').val(), $('#message').val());
$('#message').val('').focus();
});
});
});
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
}
【问题讨论】:
-
1.错误:你有生成 hubproxy 文件吗? 我认为是错误的
-
你的服务器会生成 hubfile 吗?
-
@Tester 你能告诉我怎么做吗?集线器类在我开发服务器代码的 Windows 窗体应用程序中,并且我希望 MVC 客户端网站通过服务器的 IP 连接到集线器类。
-
@Tester 嗯怎么办?
-
stackoverflow.com/questions/39975129/… 你会发现有和没有生成代理之间的区别。休息应该清楚。也许你应该阅读docs.microsoft.com/en-us/aspnet/signalr/overview/…,然后再问任何其他问题