Skip to content

Instantly share code, notes, and snippets.

@stek29
Last active May 2, 2017 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stek29/160de1f76edc01c157f80976185b30ae to your computer and use it in GitHub Desktop.
Save stek29/160de1f76edc01c157f80976185b30ae to your computer and use it in GitHub Desktop.
SpeakToGo toys
const
http = require("http"),
WebSocketServer = require('websocket').server;
const phone_html = `
<!DOCTYPE html>
<html>
<head>
<title>PHONE SIDE</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> It works </h1>
<script>
function init() {
let wsUri = document.URL.replace("http", "ws");
var websocket = new WebSocket(wsUri);
function wsSend(obj) {
if(websocket && (websocket.readyState == 1))
websocket.send(JSON.stringify(obj));
document.body.innerHTML = JSON.stringify(obj);
}
function parseOrientEvent(evt) {
var ret = {};
['alpha', 'beta', 'gamma', 'absolute'].forEach(k=> ret[k] = evt[k]);
return ret;
}
window.addEventListener("deviceorientation", evt=>wsSend({
type: 'orientation',
payload: parseOrientEvent(evt)
}));
window.addEventListener("click", evt=>wsSend({type: 'click'}));
}
window.addEventListener("load", init, false);
</script>
</body>
</html>
`
function http_start(port) {
function onRequest(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(phone_html);
response.end();
};
var server = http.createServer(onRequest).listen(port);
console.log('HTTP Up');
return server;
}
function ws_start(server) {
wsServer = new WebSocketServer({
httpServer: server
});
var connections = [];
wsServer.on('request', function(request) {
let connection = request.accept(null, request.origin);
connections.push(connection);
connection.on('message', msg=>connections.forEach(c=>c.sendUTF(msg.utf8Data)));
connection.on('close', function(conn) {
let idx = connections.indexOf(conn);
if (idx != -1) connections.splice(idx, 1);
});
});
console.log('WS Up');
return wsServer;
}
ws_start(http_start(8980));
function patchLuck() {
this.pureLuck = function(n) {
if (!n) this.renderer.fadeTextTo(['Looking for', 'random place']);
cords = `${Math.random()*180 - 90} ${Math.random()*360 - 180}`;
this.pano.load(cords).then((u)=>{
this.processOld(cords, 1);
document.location.hash = `google.com/maps/preview/@${u.lat},${u.lng},17z`;
}).catch((u)=>{
n = n || 0;
if (n < 10) {
clearTimeout(this.luckyTimeout);
this.luckyTimeout = setTimeout(()=>this.pureLuck(n + 1), 250);
} else {
this.renderer.fadeTextTo(['Try again pls']);
}
})
};
if (this.processOld === undefined) this.processOld = this.process;
this.process = (r, n) => (`i'm feeling so lucky` === r.toLowerCase() ? this.pureLuck() : this.processOld(r, n));
}
patchLuck.bind(app)();
function wsConnect() {
// Patch controls
this.renderer.controls.enabled = false;
this.renderer.controls = new THREE.DeviceOrientationControls(this.renderer.camera, this.renderer.renderer.domElement);
this.renderer.controls.disconnect();
this.renderer.controls.enabled = true;
this.renderer.controls.websocket = new WebSocket('ws://localhost:8980');
this.renderer.controls.websocket.onmessage = function(evt) {
let parsed = JSON.parse(evt.data);
if (parsed.type === 'orientation') {
this.renderer.controls.deviceOrientation = Object.assign(new CustomEvent('deviceorientation'), parsed.payload);
} else if (parsed.type === 'click') {
let elem = this.renderer.renderer.domElement;
this.interaction.onMove(0, parseInt(elem.width/2), parseInt(elem.height/2));
this.interaction.onClicked(new CustomEvent('click'));
}
};
};
var sock = wsConnect.bind(app)();

remote-control.js and node-server.js

  • npm install websocket, node node-server.js
  • Open your-pc-ip:8980 on mobile phone (lock portrait orientation on it).
  • Paste remote-control.js to Console
  • Tilt to look around, tap to click in center

pure-luck.js

I'm feeling lucky just picks location from 55 hardcoded ones. But with this it would pick random coords, and open them, when you say I'm feeling so lucky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment