Save experiments

This commit is contained in:
Roy Hyunjin Han 2014-11-17 18:43:25 -05:00
commit 7254172545
7 changed files with 121 additions and 0 deletions

16
experiments/app.js Normal file
View file

@ -0,0 +1,16 @@
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(9000);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});

16
experiments/client0.py Normal file
View file

@ -0,0 +1,16 @@
class SocketIO(object):
def __init__(self, host, port):
pass
def on(self, event, callback):
pass
def on_news(self, data):
print(data)
self.emit('my other event', {'my': 'data'})
s = SocketIO('localhost', 9000)
s.on('news', on_news)

52
experiments/client1.py Normal file
View file

@ -0,0 +1,52 @@
import requests
def get_packets(content):
packets = []
index = 0
content_length = len(content)
while index < content_length:
index, packet_length = read_packet_length(content, index)
index, packet = read_packet(content, index, packet_length)
packets.append((packet[0], packet[1:]))
return packets
def read_packet_length(content, index):
while ord(content[index]) != 0:
index += 1
index += 1
packet_length_string = ''
while ord(content[index]) != 255:
packet_length_string += str(ord(content[index]))
index += 1
return index, int(packet_length_string)
def read_packet(content, index, packet_length):
while ord(content[index]) == 255:
index += 1
packet = content[index:index + packet_length]
return index + packet_length, packet
base_url = 'http://localhost:9000'
session = requests.Session()
# Establish engine.io connection
response = session.get(
base_url + '/socket.io/?EIO=3&transport=polling&t=1416156610842-0')
packets = get_packets(response.content)
for packet_type, packet in packets:
print packet_type, packet
packet_type, packet = packets[0]
import json
packet_json = json.loads(packet)
print packet_json
print packet_json['pingInterval']
print packet_json['pingTimeout']
print packet_json['sid']
# Establish socket.io connection
# Receive socket.io event
# Send socket.io event
# Send socket.io ping
# Receive socket.io pong

8
experiments/index.html Normal file
View file

@ -0,0 +1,8 @@
<script src='/socket.io/socket.io.js'></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>

Binary file not shown.

29
experiments/proxy.js Normal file
View file

@ -0,0 +1,29 @@
var proxy = require('http-proxy').createProxyServer({
target: {host: 'localhost', port: 9000}
});
var server = require('http').createServer(function(req, res) {
console.log('[REQUEST] ' + req.url);
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
print_body('[REQUEST.BODY] ', body);
});
}
var _write = res.write;
res.write = function(data) {
print_body('[RESPONSE.BODY] ', data);
_write.call(res, data);
}
proxy.web(req, res);
});
function print_body(header, body) {
var text = String(body);
console.log(header + text);
for (var i = 0; i < text.length; i++) {
console.log('body[%s] = %s = %s', i, text[i], text.charCodeAt(i));
}
}
server.listen(8000);

BIN
experiments/proxy.log Normal file

Binary file not shown.