Created
July 17, 2014 01:00
-
-
Save haiatto/3bd111680f6a0fddf379 to your computer and use it in GitHub Desktop.
前にWebSocketで通信のテストした時のコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var fs = require('fs'); | |
var WSServer = require('websocket').server; | |
var url = require('url'); | |
var clientHtml = require('fs').readFileSync('client.html'); | |
var plainHttpServer = http.createServer( function(req, res) { | |
res.writeHead(200, { 'Content-Type': 'text/html'}); | |
res.end(clientHtml); | |
}).listen(8888); | |
var webSocketServer = new WSServer({httpServer: plainHttpServer}); | |
var accept = ['localhost', '127.0.0.1']; | |
var connections=[] | |
console.log("start"); | |
// クライアントからの接続リクエスト処理 | |
webSocketServer.on('request', function (req) { | |
console.log("request"+req.origin); | |
req.origin = req.origin || '*'; | |
// if (accept.indexOf(url.parse(req.origin).hostname) === -1) | |
// { | |
// req.reject(); | |
// console.log(req.origin + ' access not allowed.'); | |
// return; | |
// } | |
// ここでコネクションが確立される | |
var websocket = req.accept(null, req.origin); | |
websocket.on('message', function(msg) { | |
//クライアントからのメッセージを処理します | |
var json = JSON.parse(msg.utf8Data); | |
console.log(json.type); | |
//return; | |
if (json.type === "accel") { | |
var acc = json.data.accelerationIncludingGravity; | |
console.log("accel " + acc); | |
//websocket.send('sended from WebSocket Server'); | |
} | |
else{ | |
console.log("unknown type " + msg.utf8Data); | |
//websocket.send('sended from WebSocket Server' + msg.utf8Data); | |
} | |
}); | |
websocket.on('close', function (code,desc) { | |
console.log('connection released! :' + code + ' - ' + desc); | |
connections = connections.filter(function(v){ | |
return v != websocket; | |
}); | |
}); | |
connections.push(websocket); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<input id="message" type="text"><button id="send">test send</button> | |
<input type="button" id="reset", value="reset"/> | |
<div id="output"></div> | |
<div id="output2"></div> | |
<script src="http://code.jquery.com/jquery.min.js"></script> | |
<script src="./numeric-1.2.6.js"></script> | |
<script> | |
var needErr_=true; | |
window.onerror = function(errorMsg, url, lineNumber){ | |
if(needErr_)alert(url+" ["+lineNumber+"] "+errorMsg);needErr_=false; | |
} | |
</script> | |
<script> | |
var webSocket; | |
(function() { | |
var userAgent = window.navigator.userAgent.toLowerCase(); | |
var appVersion = window.navigator.appVersion.toLowerCase(); | |
webSocket = new WebSocket("ws://192.168.100.99:8888"); | |
var output = document.getElementById('output'); | |
var send = document.getElementById('send'); | |
function logStr(eventStr, msg) { | |
return '<div>' + eventStr + ':' + msg + '</div>'; | |
} | |
webSocket.onmessage = function(e) { | |
//output.innerHTML += logStr('recieved', e.data); | |
}; | |
webSocket.onclose = function (e) { | |
//output.innerHTML += logStr('disconnect', e.code + ' - ' + e.type); | |
}; | |
var sumR = [0,0,0]; | |
$("#reset").click(function() { | |
sumR = [0,0,0]; | |
}); | |
window.addEventListener('devicemotion', function(evt){ | |
var data = {type:"accel", data:{ | |
acceleration:evt.acceleration, | |
accelerationIncludingGravity:evt.accelerationIncludingGravity, | |
interval:evt.interval, | |
rotationRate:evt.rotationRate, | |
} }; | |
var json = JSON.stringify(data); | |
$("#output").text(json); | |
var v = evt.accelerationIncludingGravity; | |
var nv = [v.x,v.y,v.z]; | |
var len = numeric.norm2(nv); | |
var nv2 = numeric['/'](nv,len); | |
for (var i = 0; i < 3; i++) { | |
nv2[i] = Math.floor(nv2[i]*1000)/1000; | |
} | |
var degXy = Math.atan2(nv2[0], nv2[1])/Math.PI * 180 - 90; | |
var degXz = Math.atan2(nv2[0], nv2[2])/Math.PI * 180 - 90; | |
var test = { | |
degXy:degXy, | |
degXz:degXz, | |
}; | |
$("#output2").text(JSON.stringify(test)); | |
var data = {type:"accel", data:{ | |
deg:degXy, | |
deg:degXz, | |
} }; | |
if( webSocket.readyState==1) | |
{ | |
webSocket.send(json); | |
} | |
}, true); //加速度送信(≧∇≦) | |
}()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment