JS TCP socket

Louis Charette edited this page Apr 19, 2014 · 1 revision

APE provides a cool API : bring sockets to JavaScript on the client-side.

What does it mean? You can connect to any TCP server from the browser.

#Configuration (server side) You need to include "commands/proxy.js" in ./APE_Server/scripts/main.ape.js

Ape.addEvent("init", function() {
	include("framework/mootools.js");
	include("utils/utils.js");
	include("commands/proxy.js");
});

##Security : You have to list all host/ip:port that are allowed to be contacted : edit ./APE_Server/modules/conf/proxy.conf

#Configuration (Client side)

<DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
<head>	
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<script type="text/javaScript" src="../../Clients/JavaScript.js"></script>
	<script type="text/javaScript" src="../config.js"></script>
</head>
<body>
	<div id="ape_master_container"></div>
	<script type="text/javaScript">
		APE.Config.transport = 0;
		// Initialize APE_Client
		var client = new APE.Client();
		var TCPSocket = null;
 
		client.load();
		client.addEvent('load', function() {
			TCPSocket = this.core.TCPSocket;
			this.core.start({"name":prompt('Your name?')});
		});
 
		client.addEvent('ready', function() {
			console.log("hoi");
			var socket = new TCPSocket();
			socket.open('google.com', 80);
			socket.onopen = function() {
				console.log('connected');
				socket.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n");
			}
			socket.onread = function(data) {
				var e = document.getElementById('google');
				e.innerHTML = data;
			}
			socket.onclose = function() {
				console.log("closed");
			}
		});		
	</script>
	<div id="google"></div>
</body>
</html>