int3/doppio — pull request #253 by bmcdorman (2013-08-25 19:43:02+00:00)
Title: Networking Support with WebSockets

Here is my somewhat finalized work on transparent WebSockets support in Doppio. I'm pretty confident it's stable enough to go upstream.

1. Here's a sample server program that can be used for testing:
```
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class Echo {
private static class Worker extends Thread {
private Socket s;

public Worker(final Socket s) {
this.s = s;
setDaemon(true);
}

public void run() {
try {
final InputStream in = s.getInputStream();
final OutputStream out = s.getOutputStream();

final byte[] buffer = new byte[1024];
while(s.isConnected()) {
try { Thread.sleep(100); } catch(final Exception e) {}
final int actual = in.read(buffer);
if(actual <= 0) continue;
System.out.println("Got: " + new String(buffer, 0, actual));
out.write(buffer, 0, actual);
}
} catch(final IOException e) {e.printStackTrace();}
}
}

public static void main(final String[] args) throws IOException {
final ServerSocket s = new ServerSocket(8374);
s.setReuseAddress(true);
s.setSoTimeout(1);

while(System.in.available() == 0) {
try {
final Socket client = s.accept();
System.out.println("New connection");
(new Worker(client)).start();
} catch(final SocketTimeoutException e) {}

try { Thread.sleep(1000); } catch(final Exception e) {}
}
}
}
```

2. The associated client (to run in Doppio):
```
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class EchoClient {
public static void main(String[] args) {
try {
final Socket s = new Socket("google.com", 8080);

final InputStream input = s.getInputStream();
final OutputStream output = s.getOutputStream();

output.write("Hello, World".getBytes());
while(input.available() <= 0) {}
final byte[] buffer = new byte[1024];

while(input.available() > 0) {
final int read = input.read(buffer);
System.out.println(new String(buffer, 0, read));
}

s.close();
} catch(final Exception e) {
e.printStackTrace();
}
}
}
```

3. Download [websockify](https://github.com/kanaka/websockify) and run the following:
`sudo python websockify.py 8080 localhost:8374 `

The only thing that's missing right now is tests. I'm still determining the best way to do this, since it requires external programs and dependencies. Any ideas? I imagine this pull request will sit here for a while before being merged.