使用JavaScript的SignalR客户端C#Windows应用程序(不使用任何C#代码)
我想通过将我的Windows应用程序设置为webBrowser1(与PhoneGap的想法相同),使用java脚本创建一个客户端Windows应用程序。我编写的html页面包含带有必要src的SignalrJavaScript代码 这是Windows应用程序中的代码:使用JavaScript的SignalR客户端C#Windows应用程序(不使用任何C#代码),signalr,signalr.client,Signalr,Signalr.client,我想通过将我的Windows应用程序设置为webBrowser1(与PhoneGap的想法相同),使用java脚本创建一个客户端Windows应用程序。我编写的html页面包含带有必要src的SignalrJavaScript代码 这是Windows应用程序中的代码: webBrowser1.Url = new Uri(@"D:\Signal.html"); 这是Signal.html中的src: <script src="json2.js"></script> &
webBrowser1.Url = new Uri(@"D:\Signal.html");
这是Signal.html中的src:
<script src="json2.js"></script>
<script src="jquery-1.6.4.js"></script>
<script src="jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:5539/signalr/hubs" type="text/javascript"></script>
不幸的是,它不起作用!!怎么了
SignalR.html的孔代码在这里
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" id="message" placeholder="Message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<div style="background-color:GrayText; height:30%; text-align:center; ">
<h1>Groups</h1>
<input type="text" id="name" placeholder="User Name" />
<input type="text" id="groupName" placeholder="Group Name"/>
<input type="button" id="broadcast" value="Broadcast" />
<input type="button" id="join" value="Join" />
<input type="button" id="leave" value="Leave" />
<input type="button" id="refresh" value="Leave" />
</div>
<script src="json2.js"></script>
<script src="jquery-1.6.4.js"></script>
<script src="jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:5539/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://localhost:5539/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.chatingHub;
var counterh = $.connection.ConnectionCounter;
//counterh.client.NumberOfConnter = function (count) { $("div").append(count); }
chat.client.displayText = function (name, message) {
$('#messages').append('<li>' + name + ' said: ' + message + '</li>');
};
chat.client.le = function (id) { alert(id); }
chat.client.alertJoin = function (namePersonJoined) { $('#messages').append('<li>' + namePersonJoined + " Join to the Group" + '</li>').css("background-color", "white"); }
chat.client.alertLeave = function (namePersonLeaved) { $('#messages').append('<li>' + namePersonLeaved + " Leave from the Group" + '</li>'); }
// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
$("#broadcast").click(function () {
chat.server.broadcastMessage({
Name: $('#name').val(),
Message: $('#message').val(), Group: $('#groupName').val()
});
});
$("#join").click(function () {
chat.server.join($('#groupName').val(), $('#name').val());
});
$("#leave").click(function () {
chat.server.leave($('#groupName').val(), $('#name').val());
});
$("#refresh").click(function () {
chat.server.nowID();
});
});
});
</script>
</body>
</html>
信号员简单聊天
.集装箱{
背景色:#99CCFF;
边框:厚实线#808080;
填充:20px;
利润率:20px;
}
组
$(函数(){
//设置连接的集线器URL
$.connection.hub.url=”http://localhost:5539/signalr";
//声明代理以引用中心。
var chat=$.connection.chatingHub;
var counterh=$.connection.ConnectionCounter;
//counterh.client.NumberOfConnter=函数(count){$(“div”).append(count);}
chat.client.displayText=函数(名称、消息){
$(“#messages”).append(“”+name+”表示:“+message+” ”);
};
chat.client.le=函数(id){alert(id);}
chat.client.alertJoin=函数(namePersonJoined){$('#messages')。附加(''+namePersonJoined+“加入到组”+' ')。css(“背景色”,“白色”);}
chat.client.alertLeave=函数(namePersonLeaved){$(“#消息”).append(“”+namePersonLeaved+”离开组“+” ”);}
//创建一个中心可以调用以广播消息的函数。
chat.client.addMessage=函数(名称、消息){
//Html编码显示名称和消息。
var encodedName=$('').text(name).html();
var encodedMsg=$('').text(message.html();
//将消息添加到页面。
$(“#讨论”)。追加(“”+encodedName
+“:”+encodedMsg+” );
};
//获取用户名并将其存储到消息前。
$('#displayname').val(提示('输入您的姓名:','');
//将初始焦点设置为消息输入框。
$(“#消息”).focus();
//启动连接。
$.connection.hub.start().done(函数(){
$('#sendmessage')。单击(函数(){
//在集线器上调用Send方法。
chat.server.send($('#displayname').val(),$('#message').val());
//清除文本框并重置下一条注释的焦点。
$('#message').val('.focus();
});
$(“#广播”)。单击(函数(){
chat.server.broadcastMessage({
名称:$('#Name').val(),
消息:$('#Message').val(),组:$('#groupName').val()
});
});
$(“#联接”)。单击(函数(){
chat.server.join($('#groupName').val(),$('#name').val());
});
$(“#离开”)。单击(函数(){
离开($('#groupName').val(),$('#name').val());
});
$(“#刷新”)。单击(函数(){
chat.server.nowID();
});
});
});
我认为这是CORS的问题
您的signalR客户端位于D:\signalR.html中
你的信号服务器在运行中
这意味着它是一个跨原点信号器呼叫。您需要启用CORS才能使其正常工作。“不工作”有点不太具体。非常感谢您,是的,这是CORS问题,因此我必须上传对SignalR.html的正确引用,然后它才能正常工作。