간단한 채팅
간단한 노드 채팅 예제입니다
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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4796 | jQuery | 8년 전 | 1545 | ||
| 4795 | jQuery | 8년 전 | 1449 | ||
| 4794 | jQuery | 8년 전 | 1569 | ||
| 4793 | jQuery | 8년 전 | 1623 | ||
| 4792 | jQuery | 8년 전 | 1639 | ||
| 4791 | jQuery | 8년 전 | 1800 | ||
| 4790 | jQuery | 8년 전 | 1737 | ||
| 4789 | jQuery | 8년 전 | 1600 | ||
| 4788 | jQuery | 8년 전 | 1663 | ||
| 4787 | jQuery | 8년 전 | 1476 | ||
| 4786 | jQuery | 8년 전 | 1438 | ||
| 4785 | jQuery | 8년 전 | 1652 | ||
| 4784 | jQuery | 8년 전 | 1535 | ||
| 4783 | jQuery | 8년 전 | 1241 | ||
| 4782 | jQuery | 8년 전 | 1607 | ||
| 4781 | jQuery | 8년 전 | 1348 | ||
| 4780 | jQuery | 8년 전 | 1587 | ||
| 4779 | jQuery | 8년 전 | 1993 | ||
| 4778 | jQuery | 8년 전 | 1964 | ||
| 4777 | jQuery | 8년 전 | 1540 | ||
| 4776 | jQuery | 8년 전 | 1591 | ||
| 4775 | jQuery | 8년 전 | 1981 | ||
| 4774 | jQuery | 8년 전 | 1493 | ||
| 4773 | jQuery | 8년 전 | 1418 | ||
| 4772 | jQuery | 8년 전 | 2211 | ||
| 4771 | jQuery | 8년 전 | 3625 | ||
| 4770 | PHP | 8년 전 | 5592 | ||
| 4769 | 기타 | 8년 전 | 1970 | ||
| 4768 | 기타 | 8년 전 | 1790 | ||
| 4767 | 기타 | 8년 전 | 2079 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기