Last active
December 23, 2015 03:58
-
-
Save 59naga/6576592 to your computer and use it in GitHub Desktop.
初めてから墓場までのsocket.io 〜第一回 チャットを作ろう!〜 ref: http://qiita.com/59naga/items/0ccebe5903eb4505a9c0
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
$ node -v | |
> v0.10.24 |
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 fs=require('fs'); | |
var app=require('http').createServer(server).listen(3000); | |
function server(req,res){ | |
if(req.url==='/'){ | |
res.writeHead(200,{"Content-Type":"text/html"}); | |
res.write(fs.readFileSync('./view.html')); | |
} | |
if(req.url==='/client.js'){ | |
res.writeHead(200,{"Content-Type":"text/javascript"}); | |
res.write(fs.readFileSync('./client.js')); | |
} | |
res.end(); | |
} | |
var io=require('socket.io').listen(app); | |
var manager=io.on('connection',main); | |
function main(client){ | |
var chat; | |
client.emit('join'); | |
client.on('join',function(label){ | |
chat=label; | |
client.join(label); | |
client.send('welcome to '+label); | |
}); | |
client.on('message',function(text){ | |
manager.sockets.to(chat).send(text); | |
}); | |
} | |
io.set('log level',2); |
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
$ node server |
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
<script src="/socket.io/socket.io.js"></script> | |
<script src="/client.js"></script> | |
<form><input></form> | |
<pre></pre> |
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 fs=require('fs'); | |
var app=require('http').createServer(server).listen(3000); | |
function server(req,res){ | |
if(req.url==='/'){ | |
res.writeHead(200,{"Content-Type":"text/html"}); | |
res.write(fs.readFileSync('./view.html')); | |
} | |
if(req.url==='/client.js'){ | |
res.writeHead(200,{"Content-Type":"text/javascript"}); | |
res.write(fs.readFileSync('./client.js')); | |
} | |
res.end(); | |
} | |
var io=require('socket.io').listen(app); | |
var manager=io.on('connection',main); | |
function main(client){ | |
var chat; | |
client.emit('join'); | |
client.on('join_then',function(label){ | |
chat=label; | |
client.join(label); | |
client.send('welcome to '+label); | |
}); | |
client.on('message',function(text){ | |
manager.sockets.to(chat).send(text); | |
}); | |
} | |
io.set('log level',2); |
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
<script src="/socket.io/socket.io.js"></script> | |
<script src="/client.js"></script> | |
<form><input></form> | |
<pre></pre> |
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
window.addEventListener('load',main); | |
function main(){ | |
var form=document.getElementsByTagName("form")[0]; | |
var input=document.getElementsByTagName("input")[0]; | |
var pre=document.getElementsByTagName("pre")[0]; | |
var server=io.connect(); | |
server.on('join',function(){ | |
var label=prompt("join to..."); | |
server.emit('join',label); | |
}); | |
form.addEventListener('submit',function(event){ | |
server.send(input.value); | |
event.preventDefault(); | |
}); | |
server.on('message',function(value){ | |
var text=document.createTextNode(value+"\n"); | |
pre.appendChild(text); | |
}); | |
} |
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
window.addEventListener('load',main); | |
function main(){ | |
var form=document.querySelector("form"); | |
var input=document.querySelector("input"); | |
var pre=document.querySelector("pre"); | |
var server=io.connect(); | |
server.on('join',function(){ | |
var label=prompt("join to..."); | |
server.emit('join_then',label); | |
}); | |
form.addEventListener('submit',function(event){ | |
server.send(input.value); | |
event.preventDefault(); | |
}); | |
server.on('message',function(value){ | |
var text=document.createTextNode(value+"\n"); | |
pre.appendChild(text); | |
}); | |
} |
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 client={ | |
load:function(socket){ | |
var html='<form><input></form><pre/>'; | |
$(document.body).append(html); | |
server.emit('join',prompt('join at')); | |
}, | |
message:function(text){ | |
$('pre').append(text+"\n"); | |
}, | |
}; | |
var event={ | |
'form':function(event){ | |
var text=$("input").val(); | |
server.send(text); | |
event.preventDefault(); | |
}, | |
}; | |
var server={ | |
join:function(label){ | |
if(label==''){throw new Error('join is null');} | |
var client=this; | |
client.label=label; | |
client.join(label); | |
client.send('welcome to '+label); | |
}, | |
message:function(text,label){ | |
var client=this; | |
client.manager.sockets.to(this.label).send(text); | |
} | |
}; | |
setup(server,event,client); |
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
$ npm install cable.io | |
$ node chat |
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 cable=require('cable.io'); | |
cable.clientOn('load',function(socket){ | |
var html='<form><input></form><pre/>'; | |
$(document.body).append(html); | |
server.emit('join',prompt('join at')); | |
}); | |
cable.serverOn('join',function(label){ | |
if(label==''){throw new Error('join is null');} | |
var client=this; | |
client.label=label; | |
client.join(label); | |
client.send('welcome to '+label); | |
}); | |
cable.clientOn('message',function(text){ | |
$('pre').append(text+"\n"); | |
}); | |
cable.eventOn('form','submit',function(event){ | |
var text=$("input").val(); | |
server.send(text); | |
event.preventDefault(); | |
}); | |
cable.serverOn('message',function(text,label){ | |
var client=this; | |
client.namespace.to(this.label).send(text); | |
}); | |
var socket=cable.boot(3000); | |
socket.set('log level',2); |
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
$ npm install cable.io | |
$ node chat |
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 cable=require('cable.io'); | |
cable.clientOn('load',function(socket){ | |
var html='<form><input></form><pre/>'; | |
$(document.body).append(html); | |
server.emit('join',prompt('join at')); | |
}); | |
cable.serverOn('join',function(label){ | |
if(label==''){throw new Error('join is null');} | |
var client=this; | |
client.label=label; | |
client.join(label); | |
client.send('welcome to '+label); | |
}); | |
cable.clientOn('message',function(text){ | |
$('pre').append(text+"\n"); | |
}); | |
cable.eventOn('form','submit',function(event){ | |
var text=$("input").val(); | |
server.send(text); | |
event.preventDefault(); | |
}); | |
cable.serverOn('message',function(text,label){ | |
var client=this; | |
client.namespace.to(this.label).send(text); | |
}); | |
var socket=cable.boot(3000); | |
socket.set('log level',2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment