Currently, the TCPConnection constructor implicitly opens a tcp connection. One downside to this is that a user of the api couldn't re-use the TCPConnection object for future connections. XMLHttpRequest on the other hand has open() and abort() methods. The same duality should exist for TCPConnection, thus allowing for re-use. A secondary concern is that the usage of the API is tied to the execution model of javascript with respect to concurrency. That is to say, the only good time to attach an onopen, onclose, or onread callback to the TCPConnection object is immediately following its creation. While this may not be a problem and could certainly be worked around in most cases, adding connect() would allow these callbacks to be attached at any point after the creation of the object, but before the explict call to connect().<br>
<br>The proposal then is to make the following changes:<br><br>1) The TCPConnection() constructor should take no arguments<br>2) TCPConnection should expose an additional method, connect(subdomain, port, secure), that has a method signature identical to the old constructor<br>
3) An additional readyState value be added to the Connection interface, such that the following values have the following meanings:<br> readyState 0: initialized<br> readyState 1: connecting<br> readyState 2: connected<br>
readyState 3: closed<br>4) valid state transitions are in increasing numerical order, or from 3 -> 1 when connect() is called in a closed state.<br><br>An example of using the new TCPConnection is as follows<br><br>var tcp = new TCPConnection()<br>
tcp.onopen = function(evt) { alert('opened!') }<br>tcp.onclose = function(evt) { alert('opened!') }<br>tcp.onread = function(evt) { alert('read: ' + evt.data) }<br>tcp.connect("testing", 443) // Connect to <a href="http://testing.domain.com:443">testing.domain.com:443</a><br>
<br>And an example of re-using the TCPConnection object<br><br>tcp.disconnect()<br>tcp.onopen = function(evt) { alert('opened again!') }<br>tcp.onclose = function(evt) { alert('opened again!') }<br>tcp.onread = function(evt) { alert('read (again): ' + evt.data) }<br>
tcp.connect("testing2", 443) // Connect to <a href="http://testing2.domain.com:443">testing2.domain.com:443</a><br><br>