2016-05-03 28 views
5

我正試圖在基於單個文本文件的Rebol中編寫一個簡單的聊天應用程序。 什麼是「實時」讀取文件的最佳方式? 現在我知道了這個工作:如何讀取文件實時聊天應用程序?

t1: text 600x300 wrap green black font-name font-fixed rate 1 feel[ 
    engage: func [face action event][ 
     if action = 'time [ 
      face/text: read chatText 
      show face 
     ] 
    ] 
] 

文本字段被更新每一秒與文件的內容。即使對於多個用戶也是如此,但每個用戶每秒都會讀取整個文件。 有沒有更好的方法來做這種事情?

+1

爲什麼不在讀它之前檢查文件是否被修改? –

回答

2

看看info?函數。 你可以做這樣的事情:

REBOL [] 
chat-file: %file.txt 
file-info: info? chat-file 
update-date: file-info/date 

view layout [ 
    t1: text read chat-file 600x300 wrap green black font-name font-fixed rate 1 feel [ 
     engage: func [face action event] [ 
      if all [ 
       action = 'time 
       file-info: info? chat-file 
       update-date < file-info/date 
      ] [ 
       update-date: file-info/date 
       face/text: read chat-file 
       show face 
      ] 
     ] 
    ] 
] 

但是你必須小心,如果你會寫從多個應用程序的文件。