Site logo

Socket

Creates a TCP/IP connection, or establishes a TCP/IP server.

A working example of using the socket object:

var conn = new Socket;  
var host =  "www.indesignjs.de";  
var cmd = "GET /index.html HTTP/1.1\r\nHost:"+host+"\r\n\r\n";  
var reply='';  
conn.timeout = 100;  
if (conn.open (host +':80', 'BINARY')) {  
		conn.write (cmd);  
		while (conn.connected && ! conn.eof) {  
		   reply += conn.read(1024) + "\n";  
		}  
		conn.close();  
		alert(reply);  
} 


Socket POST connection close


See also the Web page.


See also A much better alternative to the Socket object.


Example: a simple Chat server on port 1234:

function chatServer() { 
	var tcp = new Socket; 
	// listen on port 1234 
	writeln ("Chat server listening on port 1234"); 
	if (tcp.listen (1234)) {
		for (;;) {
			// poll for a new connection 
			var connection = tcp.poll(); 
			if (connection != null) {
				writeln ("Connection from " + connection.host); 
				// we have a new connection, so welcome and chat 
				// until client terminates the session 
				connection.writeln ("Welcome to a little chat!"); 
				chat (connection); 
				connection.writeln ( "*** Goodbye ***"); 
				connection.close(); 
				delete connection; 
				writeln ("Connection closed"); 
			}
		}
	}
}