仅在使用HTML和JavaScript的网站中使用Signalr [英] Using signalr in a website only with html and javascript

查看:268
本文介绍了仅在使用HTML和JavaScript的网站中使用Signalr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我不知道是否可能,但是,我有两个网站,这些网站位于不同的服务器上.其中一个应用程序是聊天,而在另一个应用程序中,我需要知道该聊天是否具有代理.如果聊天中有座席,则该聊天是打开的.并且在第一个应用程序中将出现一个按钮或绿灯,表示聊天已打开.

此第一个应用程序是在wordpress中开发的,因此我需要尝试仅将signalr与html和javascript一起使用,并通过signalr调用一种方法来检查聊天是否打开.

另一种选择是使用webapi,通过webapi调用一个方法,如果打开了聊天,则返回1,如果关闭了聊天,则返回0.但是,问题是我需要实时执行此操作,使用此选项,我需要一次又一次地重新加载页面.

在这种情况下,仅在html和javascript中可以使用signalr吗?

在此先感谢您.

Hello guys.

I don''t know if it''s possible, but, I have two websites, the websites are located in different servers. One of the applications is a chat, and in the other application I need to know if the chat have agents. If the chat have agents, then the chat is open. And in the first application will appear a button or a green light indicating that the chat is open.

This first application is developed in wordpress, so I need to try the using of signalr only with html and javascript, calling a method through signalr for checking if the chat is open.

Other option is to use a webapi, calling a method through webapi that return 1 if the chat is open or 0 if the chat is closed. But, the problem is that I need to do this in realtime, with this option, I need to reload the page over and over again.

It is possible to use signalr in this case only with html and javascript?

Thanks in advance.

推荐答案

下面是一个普通的javascript,它将定期检查服务器:

Here''s a vanilla javascript that will check a server periodically:

var timer;

function checkStatus(){
   var xhr = new XMLHttpRequest();
   xhr.open('GET','Your web service address');
   xhr.onreadystatechange = function(){
      if (xhr.readyState == 4) {
         if(req.status == 200){
            //Do something
         }
         else{
            // Handle error  
         } 
      }
   }
   xhr.send();
}

function startListening(){
    timer = setInterval(checkStatus,1000);
}

function stopListening(){
   clearInterval(timer);
}

startListening();



在加载和错误事件回调中添加逻辑,并在指示的位置添加地址.如果需要的话,将其设为POST.



Add logic to the load and error event callbacks and add the address where indicated. Make it POST if you need to.


我有解决方法.

I have the solution.


(文档).ready(function(){
var pop = new Pop();
pop.Start("http://www.url.com/signalr/hubs");
});

在Pop中,我可以连接到集线器

伪代码
函数Start(url){
var base = this;
this.connection.hub.url = url;
this.connection.hub.start().done(function(){
base.Generate(); ->我在其他网站上留言了
base.chat.server.state(); ->根据操作状态,我是否显示该消息.状态功能在另一个网站上.
}).fail(函数(原因){
console.log(连接失败:" +原因);
});

base.InitCallerMethods();
}

内森(Nathan),您的解决方案也是正确的,但是我认为最好是使用SignalR,因为您可以直接实时获得结果,而无需配置为WebAPI.

非常感谢大家.
(document).ready(function () {
var pop = new Pop();
pop.Start("http://www.url.com/signalr/hubs");
});

In Pop I have the connection to the Hub

pseudocode
function Start(url) {
var base = this;
this.connection.hub.url = url;
this.connection.hub.start().done(function () {
base.Generate(); ->I put a message on the other website
base.chat.server.state(); -> I make visible or not that message depending on the operating status. The function State is on the other website.
}).fail(function (reason) {
console.log("Connection failed: " + reason);
});

base.InitCallerMethods();
}

Nathan , your solution is correct also , but I think it is best to approach SignalR for this, because you have the results in real time directly, without configuration as WebAPI.

Thank you very much to everybody.


这篇关于仅在使用HTML和JavaScript的网站中使用Signalr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆