2010-11-05 35 views
2

我連接到遠程Web服務器以將鼠標移動存儲在數據庫中。自從將代碼放在服務器上以來,我爲這些動作編寫的處理程序已經不可思議了。我意識到這是因爲不是在本地運行它需要獲取信息,但有可能加速一點點?下面是我使用從遠程主機獲取數據時處理動畫非常不穩定

String get_users = "http://example.com/get_users.php"; 
String get_data = "http://example.com/get_data.php?user="; 
ArrayList arrows; 
PImage mouse; 
int[] user_ids; 
int num_users; 

void setup() { 
    size(1024, 768); 
    frameRate(24); 
    smooth(); 
    noStroke(); 
    mouse = loadImage("arrow-clear.png"); 
    arrows = new ArrayList(); 
    getUsers(); 

    for (int i = 0; i < num_users; i++){ 
    arrows.add(new Arrow(user_ids[i], i*400, 2*i*100)); 
    } 
} 

void getUsers(){ 
    user_ids = int(loadStrings(get_users)); 
    num_users = user_ids.length; 
    println(num_users); 
} 

void draw() { 
    background(0); 

    if (frameCount % 600 == 0){ 
    getUsers(); 
    for (int i = 0; i < num_users; i++){ 
     arrows.add(new Arrow(user_ids[i], i*400, 2*i*100)); 
    } 
    } 

    for (int i = arrows.size()-1; i >= 0; i--) { 
    Arrow arrow = (Arrow) arrows.get(i); 
    arrow.move(); 
    if (arrow.finished()) { 
     arrows.remove(i); 
    } 
    } 

} 

class Arrow { 
    String[] all_moves, move_pairs, new_moves; 
    int[] moves; 
    float x; 
    float y; 
    int id; 
    int i = 0; 
    Boolean is_done = false; 

    Arrow(int tempID, float tempX, float tempY) { 
    all_moves = loadStrings(get_data + tempID); 
    id = tempID; 
    x = tempX; 
    y = tempY; 
    if (all_moves.length > 0){ 
     move_pairs = shorten(split(all_moves[0], "|")); 
    } 
    } 

    void move() { 
    if (move_pairs != null){ 
     if (i < move_pairs.length){ 
     moves = int(split(move_pairs[i], ",")); 
     image(mouse, moves[0], moves[1]); 
     ++i; 
     } else { 
     all_moves = loadStrings(get_data + id); 
     if (all_moves.length > 0){ 
      new_moves = shorten(split(all_moves[0], "|")); 
      for (int j = 0; j < new_moves.length; j++){   
      move_pairs = append(move_pairs, new_moves[j]); 
      } 
      println(move_pairs); 
     } else { 
      is_done = true; 
     } 
     } 
    } else { 
     is_done = true; 
    } 
    } 

    boolean finished() { 
    if (is_done) { 
     return true; 
    } else { 
     return false; 
    } 
    } 
} 

編輯代碼:澄清:處理應用程序做所有的動畫是在本地運行。鼠標的X和Y點是從服務器下載的唯一東西。

+0

在客戶端使用Javascript或Flash進行動畫 – 2010-11-05 15:20:51

+0

也許配置您的服務器以較小的塊發送? – Dereleased 2010-11-05 15:26:15

回答

1

您希望將所有移動數據(或其大塊)下載到客戶端,讓客戶端完成所有動畫的工作。

0

我懷疑在每一幀下載運動數據是個好主意。如果您不需要這種詳細的響應性,請定期從服務器獲取一批移動,並將其排列爲繪製方法。否則,請確保服務器只發送所需的數據。我意識到你只使用從服務器獲取的第一行數據 - all_moves[0]。如果確實只有一條線 - 很好。

您應該考慮使用createInput(URL)並從該流中讀取,這樣您就不需要爲請求的每個移動打開一個新的輸入流,但是您的服務器端代碼必須能夠維護流並連續寫入它們。