Skip to content

Instantly share code, notes, and snippets.

@okaq
Created May 27, 2014 16:59
Show Gist options
  • Save okaq/f4e85c533c34c650d72d to your computer and use it in GitHub Desktop.
Save okaq/f4e85c533c34c650d72d to your computer and use it in GitHub Desktop.
XHR Binary Data : Send & Recieve
<!DOCTYPE html>
<html lang="en">
<head id="zeta">
<title>okaq</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=1920,height=1080,initial-scale=1" />
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAMklEQVR4nGLZwPmCgZaAiaamj1owasGoBaMWjFowasGoBaMWjFowasGoBVQEgAAAAP//cmEB5DRz+QkAAAAASUVORK5CYII=" />
<style type="text/css">
html,body{width:1920px;height:1080px;background-color:rgba(16,4,8,1.0);}
</style>
<script type="text/javascript">
// ok
console.log("okaq vobu foti!");
// async load
(function() {
var a = {
"load": function() {
console.log("a load");
g.init();
}
}
window.addEventListener("load", a.load, false);
})();
// game
var g = {
"init": function() {
console.log("g init");
console.log(window.location);
c.init();
g.word = u.word(97, 123, ((Math.random()*32)+32) >>> 0);
console.log("g word: " + g.word);
g.hex = u.hex();
console.log("g hex: " + g.hex);
g.buff = u.buff(g.word);
console.log("g buff: ");
console.log(g.buff);
d.init();
x.init();
x.chip(g.buff);
}
}
// dom
var d = {
"init": function() {
d.alpha = document.getElementById("alpha");
console.log("d alpha: " + d.alpha);
d.add("beta", new dim(786, 786), new pos(16, 16));
console.log("d beta: ");
console.log(d.beta, d.beta.can, d.beta.con);
},
"add": function(n0, dim0, pos0) {
d[n0] = {};
d[n0].can = document.createElement("canvas");
d[n0].con = d[n0].can.getContext("2d");
}
}
// dimension
var dim = function(w0, h0) {
this.w = w0;
this.h = h0;
}
// position
var pos = function(x0, y0) {
this.x = x0;
this.y = y0;
}
// webrtc
var w = {
"init": function() {
// peer conn
w.pc = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
// options
w.opt = {"options": [{"RtpDataChannels":true}]};
// get ice servers
w.ice = null;
}
}
// xhr
var x = {
"init": function() {
x.req = new XMLHttpRequest();
// x.req.responseType = "arraybuffer";
console.log("x req: ");
console.log(x.req);
},
"chip": function(b0) {
// xhr post bytebuffer to web server chipper handler
x.req.open("POST", "/chip", true);
x.req.responseType = "string";
x.req.onload = function(e) {
console.log(e);
console.log(x.req.response);
x.buff = u.buff(x.req.response);
console.log(x.buff); // arraybuffer response type not supported?
};
// form data sends string "[object Uint8Array]" ;)
// var fd = new FormData();
// fd.append("data", b0);
// set request header
x.req.setRequestHeader("Content-Type", "application/octet-stream");
// x.req.setRequestHeader("Content-Length", b0.length);
x.req.send(b0);
}
}
// color
var c = {
"init": function() {
c.data = new Uint8Array(4);
c.rand();
},
"rand": function() {
for (var i = 0; i < c.data.length; i++) {
c.data[i] = c.rb();
}
},
"rb": function() {
return ((Math.random() * 255) >>> 0);
},
"css": function() {
s0 = "rgba(";
for (var i = 0; i < (c.data.length - 1); i++) {
s0 += c.data[i].toString();
s0 += ",";
}
s0 += (c.data[c.data.length - 1] / 255).toString();
s0 += ")";
return s0;
}
}
// unicode
var u = {
"rand": function(lo, hi) {
var d0 = (hi - lo) >>> 0;
var d1 = ((Math.random() * d0) + lo) >>> 0;
return String.fromCharCode(d1);
},
"word": function(lo, hi, n0) {
var r0 = [];
for (var i = 0; i < n0; i++) {
r0.push(u.rand(lo, hi));
}
return r0.join("");
},
"hex": function() {
var n0 = 1 << 31;
var n1 = (Math.random() * n0) >>> 0;
return n1.toString(16);
},
"buff": function(s0) {
// convert []char to uint8buffer
var r0 = new Uint8Array(s0.length);
for (var i = 0; i < s0.length; i++) {
var b0 = s0.charCodeAt(i);
if (b0 > 255) { b0 = 255 };
r0[i] = b0;
}
return r0;
}
}
console.log(window);
// explicit names : window.okaq_color
</script>
</head>
<body id="alpha">
</body>
</html>
/*
* Golang Web Server
* WebRTC PSY GAME
* AQ <[email protected]>
* 2014-05-01
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"
)
const (
MOTD = "vobu"
AINA = "aina.html"
BORU = "boru.html"
DOKA = "doka.html"
FOTI = "foti.html"
PORT = ":8008"
APPCACHE = "vobu.appcache"
)
var (
Cache []byte // mutex locked on read?
err error
)
// Load HTML5 web app into cache
func Load() {
Cache, err = ioutil.ReadFile(AINA)
if err != nil {
log.Fatal(err)
}
}
// Any manual cache will need to be RWLock'd
// golang file server api uses sendfile
// and checks if file is modified
// Web server
func Serve() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// fmt.Printf("Request: %+v.\n", r)
// w.Write(Cache)
// r.Body.Close()
// http.ServeFile(w, r, AINA)
// http.ServeFile(w, r, BORU)
// http.ServeFile(w, r, DOKA)
http.ServeFile(w, r, FOTI)
})
http.HandleFunc("/vobu.appcache", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, APPCACHE)
})
http.HandleFunc("/chip", func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r)
fmt.Println(r.Body)
b0, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(b0))
// var p []byte
// r.Body.Read(p)
// fmt.Println(p)
/*
r.ParseForm()
for k := range r.PostForm {
fmt.Println(k, r.PostForm[k])
}
r.ParseMultipartForm(1<<24)
fmt.Println(r.MultipartForm.Value["data"][0])
for i := range r.MultipartForm.Value["data"][0] {
fmt.Println(i, r.MultipartForm.Value["data"][0][i])
}
fmt.Println(r.MultipartForm)
*/
w.Header()["Content-Type"] = []string{"application/octet-stream"}
w.Header()["Content-Length"] = []string{"2"}
w.Write([]byte{97, 98})
})
log.Fatal(http.ListenAndServe(PORT, nil))
}
func main() {
fmt.Printf("Greetings %s!\nStarting web server on port: %s.\n", MOTD, PORT)
Load()
runtime.GOMAXPROCS(runtime.NumCPU())
Serve()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment