Open chenchenbox opened 10 years ago
目前卡在每次data事件被處發時,所得到的資料都沒有很完整,我需要等到screen收集完才可以執行下一個ptt command.
根據node net(http://nodejs.org/api/net.html#net_class_net_socket) :
createConnection() is a factory method, which returns a new 'net.Socket' and connects to the supplied address and port. Has the same events as 'net.Socket'.
而net.Socket為(http://nodejs.org/api/net.html#net_class_net_socket):
Class: net.Socket This object is an abstraction of a TCP or UNIX socket. net.Socket instances implement a duplex Stream interface. They can be created by the user and used as a client (with connect()) or they can be created by Node and passed to the user through the 'connection' event of a server.
根據node stream(http://nodejs.org/api/stream.html#stream_class_stream_duplex):
Class: stream.Duplex Duplex streams are streams that implement both the Readable and Writable interfaces. See above for usage.
且Readable interface 的其中一個 event 為 'end' (http://nodejs.org/api/stream.html#stream_event_end) 和net.socket的 'end' event (http://nodejs.org/api/net.html#net_event_end) 用途不一樣.
似乎雖為繼承關係,event 被實作的方式不同,不明白為何,如果可以就Readable interface的定義,我可以就批踢踢傳完資料時,直接觸發事件。但net.socket的'end'就是不一樣的樣子.
補: interface 與 abstract class 的差異. => http://www.flag.com.tw/book/cento-5105.asp?bokno=FS720&id=312
Readable interface 'end' event (http://nodejs.org/api/stream.html#stream_event_end) :
This event fires when there will be no more data to read.
Note that the end event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by calling read() repeatedly until you get to the end.
var readable = getReadableStreamSomehow(); readable.on('data', function(chunk) { console.log('got %d bytes of data', chunk.length); }) readable.on('end', function() { console.log('there will be no more data.'); });
net.socket 'end' event (http://nodejs.org/api/net.html#net_event_end):
Emitted when the other end of the socket sends a FIN packet.
By default (allowHalfOpen == false) the socket will destroy its file descriptor once it has written out its pending write queue. However, by setting allowHalfOpen == true the socket will not automatically end() its side allowing the user to write arbitrary amounts of data, with the caveat that the user is required to end() their side now.
結論: 經過測試, timeout參數設定為500ms較為OK., 應當更改程式, 當timeout觸發時才執行傳送下一個ptt指令,而非一接獲資料就傳送.
可以考慮試試看連線改用'node-telnet-client',
'node-telnet-client'(https://github.com/mkozjak/node-telnet-client) 實作了telnet client端,應該是根據RFC 854 spec(http://tools.ietf.org/html/rfc854) , 根據作者的回信,利用偵測telnet sever回傳的data偵測prompt, 來判斷是否已經收集完資料.
信件紀錄: Ask from chenchen:
Reply from mkozjak(kozjakm1@gmail.com):
補: Event: 'timeout' (http://nodejs.org/api/net.html#net_event_timeout):
補: 經過測試, timeout參數設定為500ms較為OK., 應當更改程式, 當timeout觸發時才執行傳送下一個ptt指令,而非一接獲資料就傳送.