간단한 채팅
간단한 노드 채팅 예제입니다
1. 노드설치 후, 서버 js 실행
// server.js
var express = require('express');
var app = express();
var http = require('http').Server(app); //1
var io = require('socket.io')(http); //1
app.get('/',function(req, res){ //2
res.sendFile(__dirname + '/client.html');
});
var count=1;
io.on('connection', function(socket){ //3
console.log('user connected: ', socket.id); //3-1
var name = "user" + count++; //3-1
io.to(socket.id).emit('change name',name); //3-1
socket.on('disconnect', function(){ //3-2
console.log('user disconnected: ', socket.id);
});
socket.on('send message', function(name,text){ //3-3
var msg = name + ' : ' + text;
console.log(msg);
io.emit('receive message', msg);
});
});
http.listen(3000, function(){ //4
console.log('server on!');
});
2. 메세지 입력 화면
// client.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chat</title>
<style>
.chat_log{ width: 95%; height: 200px; }
.name{ width: 10%; }
.message{ width: 70%; }
.chat{ width: 10%; }
</style>
</head>
<body>
<div>
<textarea id="chatLog" class="chat_log" readonly></textarea>
</div>
<form id="chat">
<input id="name" class="name" type="text" readonly>
<input id="message" class="message" type="text">
<input type="submit" class="chat" value="chat"/>
</form>
<div id="box" class="box">
<script src="/socket.io/socket.io.js"></script> <!-- 1 -->
<script src="//code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io(); //1
$('#chat').on('submit', function(e){ //2
socket.emit('send message', $('#name').val(), $('#message').val());
$('#message').val('');
$('#message').focus();
e.preventDefault();
});
socket.on('receive message', function(msg){ //3
$('#chatLog').append(msg+'\n');
$('#chatLog').scrollTop($('#chatLog')[0].scrollHeight);
});
socket.on('change name', function(name){ //4
$('#name').val(name);
});
</script>
</body>
</html>
상세설명
https://onlyfor-me-blog.tistory.com/99
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5036 | 웹서버 | 4년 전 | 2144 | ||
| 5035 | 웹서버 | 4년 전 | 2173 | ||
| 5034 | 웹서버 | 4년 전 | 2404 | ||
| 5033 | 웹서버 | 4년 전 | 1780 | ||
| 5032 | 웹서버 | 4년 전 | 2139 | ||
| 5031 | 웹서버 | 4년 전 | 2518 | ||
| 5030 | MySQL | 4년 전 | 2887 | ||
| 5029 | 웹서버 | 4년 전 | 7184 | ||
| 5028 | 웹서버 |
kerimdoor
|
4년 전 | 1634 | |
| 5027 | OS | 4년 전 | 6559 | ||
| 5026 | PHP |
|
4년 전 | 4642 | |
| 5025 | JavaScript |
|
4년 전 | 2405 | |
| 5024 | MySQL |
|
5년 전 | 3822 | |
| 5023 | MySQL | 5년 전 | 3033 | ||
| 5022 | PHP | 5년 전 | 2326 | ||
| 5021 | PHP |
|
5년 전 | 3252 | |
| 5020 | PHP | 5년 전 | 4308 | ||
| 5019 | PHP | 5년 전 | 4333 | ||
| 5018 | 웹서버 | 5년 전 | 6706 | ||
| 5017 | 기타 |
HappyTank
|
5년 전 | 5071 | |
| 5016 | MySQL | 5년 전 | 3092 | ||
| 5015 | 기타 | 5년 전 | 2279 | ||
| 5014 | 기타 | 5년 전 | 2839 | ||
| 5013 | 기타 | 5년 전 | 2256 | ||
| 5012 | 기타 | 5년 전 | 2267 | ||
| 5011 | 기타 | 5년 전 | 2220 | ||
| 5010 | 기타 | 5년 전 | 1779 | ||
| 5009 | 기타 | 5년 전 | 2388 | ||
| 5008 | 기타 | 5년 전 | 2020 | ||
| 5007 | 기타 | 5년 전 | 1982 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기