使用Websocket客户端的WCF全双工应用程序

时间:2015-08-11 21:42:14

标签: javascript web-services wcf websocket

我们使用一种方法创建了WCF Web服务。服务托管在外部服务器上,即Windows Server 2012和IIS 8.0。

WCF服务网址:http://184.106.9.214/WCFReportingService/Service1.svc

WCF方法:

 public void ProcessReport()
    {
        for (int i = 1; i <= 100; i++)
        {
            // some logic to process the report
            Thread.Sleep(100);
            // Get the callback channel to send messages to the client
            OperationContext.Current.
                GetCallbackChannel<IReportServiceCallback>().Progress(i);
        }
    }

我们正在尝试使用HTML5和JavaScript创建客户端。以下是我们用于启动连接的逻辑。

 ws = new WebSocket("ws://localhost/WCFReportService/Service1.svc");
            alert(ws);

            ws.onopen = function () {

                // Web Socket is connected, send data using send()
                ws.send("Message to send");
                alert("Message is sent...");

                $("#spanStatus").text("connected");
            };
            ws.onmessage = function (evt) {
                var received_msg = evt.data;
                alert("Message is received...");

                $("#spanStatus").text(evt.data);
            };
            ws.onerror = function (evt) {
                $("#spanStatus").text(evt.message);
            };
            ws.onclose = function () {
                // websocket is closed.
                alert("Connection is closed...");

                $("#spanStatus").text("disconnected");
            };

我们无法与服务器建立连接。我们认为它可能与客户端web.config文件有关。但我们不确定如何实现或建立连接。

任何人都可以帮助我们构建客户端 - 服务器连接吗?

感谢。

1 个答案:

答案 0 :(得分:1)

它可能会帮助我遇到类似问题的人。以下是我使用的链接,我能够使其正常工作。

简介2:http://www.codeproject.com/Articles/618032/Using-WebSocket-in-NET-4-5-Part-2 简介3:http://www.codeproject.com/Articles/619343/Using-WebSocket-in-NET-4-5-Part-3

相关问题