【发布时间】:2013-07-08 07:21:45
【问题描述】:
我想使用SignalR 来识别WCF service 上的Jquery 客户端,即使没有请求也可以向他发送消息。 (即在客户端向服务发送第一个请求后,服务可以知道他并向他发送消息)。
我不知道这是否是最好的方法,但这是我能找到的。 (除了仅在 VS 2012 中支持的 WebSocket)。
我在Service的Global文件中添加了如下函数:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
我创建了 Chat.cs:
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.All.addMessage(message);
}
}
在JS 项目中我添加了SignalR 的JS 文件:
<script src="../JavaScript/jquery.signalR-1.1.2.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
使用它的函数:
function Chat() {
$(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) {
alert(message);
};
// Start the connection
$.connection.hub.start().done(function () {
// Call the chat method on the server
chat.server.send('aa');
});
});
}
如果这个问题很愚蠢,这对我来说很抱歉,但是 JS 的 SignalR 应该如何知道服务,我应该在哪里定义他? (这需要跨域)
(变量$.connection.chat未定义)
我确定我错过了一些事情,尤其是他如何通过 SignalR 链接服务和 JS?
【问题讨论】:
-
您能否详细解释一下 WCF 如何融入图片?您在这里展示的只是基本的 SignalR 客户端/服务器通信。另外,请务必查看示例:github.com/SignalR/Samples
标签: javascript jquery html wcf signalr