Created
November 28, 2011 07:14
-
-
Save kanreisa/1399446 to your computer and use it in GitHub Desktop.
node.js / socket.io SSL接続サンプル (サーバ, クライアント)
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 PORT = 10443; | |
var SSL_KEY = '___.key'; | |
var SSL_CERT= '___.cert'; | |
var fs = require('fs'); | |
var io = require('socket.io').listen(PORT, { | |
key : fs.readFileSync(SSL_KEY).toString(), | |
cert : fs.readFileSync(SSL_CERT).toString() | |
}); | |
io.enable('browser client minification'); | |
io.set('log level', 3); | |
io.sockets.on('connection', function (socket) { | |
//接続してきたらウェルカムメッセージを送信する | |
setTimeout(function () { | |
socket.emit('message', 'このメッセージが見えていればメッセージの受信に成功していまーす'); | |
}, 500); | |
//誰かが接続してきたことをみんなに知らせる | |
socket.broadcast.emit('message', 'だれかが来たようです'); | |
//誰かが切断したことをみんなに知らせる | |
socket.on('disconnect', function () { | |
socket.broadcast.emit('message', 'だれかが出て行きました'); | |
}); | |
}); |
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<meta http-equiv="Content-Language" content="ja" /> | |
<meta http-equiv="X-UA-Compatible" content="chrome=1; IE=8; IE=edge" /> | |
<title>node.js / socket.io SSL接続サンプル</title> | |
<script type="text/javascript" src="https://localhost:10443/socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
setTimeout(function () { | |
// 接続を初期化 | |
var socket = io.connect('https://localhost:10443'); | |
// 接続したとき | |
socket.on('connect', function () { | |
document.body.innerHTML = new Date().toString() + '<span>接続が確立しましたー</span><br>'; | |
}); | |
// メッセージを受信したとき | |
socket.on('message', function(message) { | |
document.body.innerHTML = new Date().toString() + '<span>' + message + '</span><br>' + document.body.innerHTML; | |
}); | |
}, 100); | |
</script> | |
<style type="text/css"> | |
body { | |
background-color: black; | |
font-size : 12px; | |
color : gray; | |
line-height : 40px; } | |
body > span { | |
color : lightgray; | |
font-size : 24px; | |
margin-left : 10px; } | |
body > span:first-child { | |
color : #feb2ad; } | |
</style> | |
</head> | |
<body> | |
オフライン | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment