2016-07-25 107 views
-1

即時通訊使用Javascript來顯示用Java生成的信息。 我談了10,000個數據,所有這些數據都是用Java生成的,並且使用隨機值生成了測試內容。那麼我想在我的JavaScript頁面中看到這10,000個值。使用服務器Java&Javascript客戶端與Apache Thrift的數組

我已經有一個簡單的服務器Java和客戶端Javascript共享2雙打。

這是2個雙打的節儉文件:

namespace java test_thrift 
service test{ 
    double number(1:double n1, 2:double n2) 
} 

這是我的JavaScript客戶端

function calc() { 
var transport = new Thrift.Transport("/service"); 
var protocol = new Thrift.Protocol(transport); 
var client = new testClient(protocol); 

var workbench = Math.random()*1000; 
    try { 
     result = client.number(workbench); 
     $('#result').val(result); 
     $('#result').css('color', 'black'); 
     document.getElementById("demo").innerHTML = result; 
    } catch(ouch){ 
     $('#result').val(ouch.why); 
     $('#result').css('color', 'red'); 
    } 
} 

Im的發送隨機只拿到的範圍內碼返回。實施例:1,從3的值返回到圖9中,2返回一個9至15個值等

而在Java我有一個testHandler類:

public double number(double n1, double n2) throws TException { 
    //System.out.println(n1 + " - " + n2); 
    Random rnd = new Random(); 
    n1 = rnd.nextDouble() * 10 + 1; 
    n2 = rnd.nextDouble() * 15 + 10; 
    return n2; 
} 

那麼這將返回值1。我想在我的Javascript頁面中看到所有內容。但有10,000個元素。我怎樣才能做到這一點?

此外,我想補充的是,最終的結構來分享的是這樣的:

dis[10000][3]={ 
    ABC,12.5,13.5, 
    ACD,14.4,11.5, 
    .....ETC......} 

林卡住

發現這一點,但我不知道如何得到它的工作:/

namespace java test_thrift 

typedef list<double> Vector 

struct test 
{ 
    1:i32 rows, 
    2:i32 cols, 
    3:list<Vector> data, 
} 
+0

鑑於我理解這個問題的權利:怎麼樣使用'名單'和'pair'是一個'struct pair {1:double one,2:double two}'?通過這種方式,您可以通過一次呼叫傳遞儘可能多的數據。如果chaning服務器的IDL不是一個有效的選擇,那麼你必須執行10000次調用(這將是更低的性能) – JensG

+0

準確地說,我只想進行1次調用並傳遞所有信息。但我不知道如何「名單」的作品。我從來沒有用過它。你知道一些例子嗎? –

回答

0

所以,我設法解決它。這裏是一個客戶JavasCript,它使reequest和Java服務器回饋一個10000 x 18矩陣。

節儉文件:

命名空間中的Java test_thrift

struct Cell { 
    1: string did 
    2: double l_x 
    3: double l_y 
    4: double l_z 
    5: double m_x 
    6: double m_y 
    7: double m_z 
    8: double a_x 
    9: double a_y 
    10: double a_z 
    11: double g_x 
    12: double g_y 
    13: double g_z 
    14: string d_t 
    15: double tp 
    16: double r_q 
    17: string o_m 
    18: double b_v 
} 

service test{ 
    list<Cell> number(1 : i16 req) 
} 

然後在Javascript中我只發送請求:

function calc() { 
var transport = new Thrift.Transport("/service"); 
var protocol = new Thrift.Protocol(transport); 
var client = new pruebaClient(protocol); 


var workbench = Math.random()*1000; 
var div = document.getElementById('deltat'); 


    try { 

     result = client.number(1); 
     div.innerHTML = div.innerHTML + '\nReady'; 

    } catch(ouch){ 

     $('#result').val("ERROR"); 
     $('#result').css('color', 'red'); 
    } 
} 

而且在結果y的[10000] [18]答案並像這樣打印:

for (var i = result.length - 1; i >= 0; i--) { 
     //div.innerHTML = div.innerHTML + '\n' + result[i]; 
     div.insertAdjacentHTML('beforeend', '\n'+result[i].did+' '+result[i].l_x+' '+result[i].l_y+' '+result[i].l_z+' '+result[i].m_x+' '+result[i].m_y+' '+result[i].m_z+' '+result[i].a_x+' '+result[i].a_y+' '+result[i].a_z+' '+result[i].g_x+' '+result[i].g_y+' '+result[i].g_z+' '+result[i].d_t+' '+result[i].tp+' '+result[i].r_q+' '+result[i].o_m+' '+result[i].b_v); 
    }; 
    } 

最後,在我的Java服務器的處理程序是這樣的:

public class TestHandler implements test.Iface { 
    public Random rnd = new Random(); 
    public static List<Cell> p = new ArrayList<Cell>(); 

    public void test() { 
    for (int i = 0; i < 10000; i++) { 
     Cell a = new Cell(); 
     a.did = "SomeString"; 
     a.l_x = rnd.nextDouble()*10+1; 
     a.l_y = rnd.nextDouble()*10+1; 
     a.l_z = 0.0; 
     a.m_x = 0.0; 
     a.m_y = 0.0; 
     a.m_z = 0.0; 
     a.a_x = 0.0; 
     a.a_y = 0.0; 
     a.a_z = 0.0; 
     a.g_x = 0.0; 
     a.g_y = 0.0; 
     a.g_z = 0.0; 
     a.d_t = "String here"; 
     a.tp = 0.0; 
     a.r_q = 0.0; 
     a.o_m = "A"; 
     a.b_v = 0.0; 
     p.add(a); 
    } 
} 
@Override 
public List<Cell> number(short req) throws TException { 
    test(); 
    return ips.ReminderBeep.list_d; 
} 

我希望這將是有用的人

+0

您是否意識到您的答案與您自己的問題甚至不相符? – JensG

+0

爲什麼不呢?我在開始時說過,我會像這樣的形式發送一個矩陣: 'dis [10000] [3]'。你不讀那個嗎?我只是改變「3」爲「18」。怎麼了?。你回答從Javascript發送數據到Java,我提到我想要的是相反的。 –

1

使用此IDL文件

namespace java test_thrift 

struct MyPair { 
    1: double one 
    2: double two 
} 

service test{ 
    list<double> number(1 : list<MyPair> data) 
} 

的功能,然後調用,像這樣:然後

var list = []; 
for(var i = 0; i < 10000; ++i) { 
    list.push({ 
     one : Math.random()*1000, 
     two : Math.random()*1000 
    }); 
} 
result = client.number(list); 

結果應該是當然的給定,返回的值列表中的服務器端相應的執行。

+0

沒關係,如果我重新使用服務器,只需更改處理程序?我試圖打印列表的大小,但我不能。 JavaScript的文件總是去捕捉。對不起,這是煩人的,我是新的節儉,但真的很感謝你的幫助。 –