从 Websocket 函数中获取值 [英] Getting Values out of Websocket Functions

查看:48
本文介绍了从 Websocket 函数中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,该项目需要在网络服务器上从 C++ 代码到 JavaScript 进行通信.目前,我已正确发送数据并且正在接收数据,但问题是我无法使用内部(onmessage)函数之外的数据.这仍然可以很好地覆盖网页元素,但是我尝试构建的图表无法获取实时数据.如果我将图表的代码放在内部函数中,整个程序就会冻结,我也无法从函数中取出变量以便在父函数中使用它.现在,我只想从内部函数中获取数据,以便我可以使用它.那么,有什么方法可以让我从内部函数中提取这些数据并在父函数中使用它吗?

I am working on a project which needs to get communication from C++ code to JavaScript on a webserver. Currently, I have data sending properly and it's being received but the issue is that I cant use the data outside of the inner(onmessage) function. This still works fine to overwrite elements of the webpage, but the charts I'm trying to build cant get the live data. If I put the code for the chart inside the inner function the entire program freezes and I can't get the variable out of the function for me to use it in the parent either. Right now, I just want to get the data out of the inner function so I can use it. So, is there any way for me to pull that data out of the inner function and use it in the parent function?

这是我的代码:

{
     var x;

     var ws = new WebSocket('ws://localhost:10011/');
     ws.onmessage = function(event) 
     {    
         x = event.data;
         var testing = document.getElementById('InnerFunctionOutput');
         testing.innerHTML = "Run Time: " + x;
     }

     var testing = document.getElementById('ParentFunctionOutput');
     testing.innerHTML = "Run Time: " + x;
}

当我运行此代码时,内部函数的输出是恒定的数据流,而父函数的输出是常量 1.如果有人能帮我找到解决方案,将不胜感激.

When I run this code the output from the inner function is the constant stream of data and the output from the parent is a constant 1. If anyone could help me find a solution, it would be greatly appreciated.

另一种解决方案是将图表函数放在 websocket 函数中.这样,我就不必从函数中获取数据.然而,这个解决方案有其自身的一系列问题.如果您对该部分感兴趣,我将在下面放置一个链接到一个线程,在该线程中我会询问此替代解决方案.

One alternative solution is to put the functions for the charts inside the websocket function. That way, I wouldn't have to get data out of the function. however, this solution has its own set of problems. I will put a link below to a thread where I ask about this alternate solution if you are interested in that part.

Plotly 和 Websockets

任何帮助将不胜感激.

谢谢,卢克

推荐答案

我找到了问题的解决方案.为了从 websocket 函数中获取数据,我在页面上覆盖了一个文本元素,并简单地使该元素不可见.之后,只需阅读文本元素即可将其放入其余代码中.但是,可能有更好的方法来做到这一点,所以请让我知道是否有.

I found a solution to the problem. To get the data out of the websocket function I overwrote a text element on the page and simply made that element invisible. After that, just read the text element in order to get it into the rest of the code. However, there could be a better way of doing this so please let me knw if there is.

此外,我在使用此解决方案时遇到了一个问题.当我最初尝试使用正常格式 document.getElementById('some id').innerHTML = some data; 时,它不起作用.但是当添加窗口"时到开头 window.document.getElementById('some id').innerHTML = some data; 它刚刚起作用.

Also, there is one issue I ran into with this solution. When I originally tried this with normal formatting document.getElementById('some id').innerHTML = some data; it didn't work. But when with adding "window" to the beginning window.document.getElementById('some id').innerHTML = some data; it just worked.

这篇关于从 Websocket 函数中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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