Created
September 15, 2012 05:50
-
-
Save atomaths/3726500 to your computer and use it in GitHub Desktop.
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" /> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
<script> | |
if (window.MozWebSocket) { | |
window.WebSocket = window.MozWebSocket;; | |
} | |
$(document).ready(function() { | |
$("#user_id").keyup(function(e) { | |
if (e.keyCode == 13) signIn(); | |
}); | |
$("#btn_login").click(signIn); | |
$('#user_id').focus(); | |
}); | |
function signIn() | |
{ | |
if ($.trim($('#user_id').val()).length == 0) { | |
return; | |
} | |
var url = window.location.host + '/chat'; | |
var ws; | |
if (window.WebSocket) { | |
ws = new WebSocket('ws://' + url); | |
} else { | |
alert_msg("Your browser doesn't support WebSocket."); | |
return; | |
} | |
ws.onopen = function() { // Connected | |
$('#login_page').hide(); | |
$('#chat_page').show(); | |
$('#send').focus(); | |
ws.send($.trim($('#user_id').val())); | |
// for Mozilla | |
$('#send').removeAttr('disabled'); | |
$('#send').focus(); | |
$('#btn_send').removeAttr('disabled'); | |
}; | |
ws.onclose = function() { // Closed | |
alert_msg('채팅이 종료되었습니다.', false); | |
$('#send').attr('disabled', true); | |
$('#btn_send').attr('disabled', true); | |
}; | |
ws.onmessage = function(e) { // Received | |
print(e.data); | |
}; | |
// Send message | |
send = function() { | |
ws.send($('#send').val()); | |
$('#send').val(''); | |
$('#send').focus(); | |
}; | |
$("#send").keyup(function(e) { | |
if (e.keyCode == 13) send(); | |
}); | |
$('#btn_send').click(send); | |
$('#btn_close').click(function() { | |
ws.close(); | |
}); | |
} | |
function print(msg) | |
{ | |
$('#chat_window').append(msg + '<br>'); | |
$('#chat_window').scrollTop($('#chat_window').innerHeight()); | |
} | |
function alert_msg(msg, fadeout) | |
{ | |
fadeout = (typeof fadeout == 'undefined') ? true : false; | |
$('#msg').text(msg); | |
$('#msg').show(); | |
if (fadeout) { | |
$('#msg').fadeOut(5000); | |
} | |
} | |
</script> | |
<style> | |
body { font: 12px Verdana, Arial, Helvetica, sans-serif; } | |
</style> | |
</head> | |
<body> | |
<div id="msg" style="display: none; color: #ffffff; background-color: #eb574a; padding: 5px; margin-bottom: 10px; font-weight: bold;"></div> | |
<div id="login_page"> | |
<input type="text" id="user_id" placeholder="아이디" /> | |
<input type="button" id="btn_login" value="로그인" /> | |
</div> | |
<div id="chat_page" style="display: none;"> | |
<div id='chat_window' style="border: 1px solid; width: 98%; height: 300px; padding: 5px; line-height: 14px; overflow-y: auto;"></div> | |
<div style="margin-top: 3px;"> | |
<input type="text" id="send" size="70" placeholder="메시지를 입력하세요" /> | |
<input type="button" id="btn_send" value="보내기" /> | |
<input type="button" id="btn_close" value="종료" /> | |
</div> | |
</div> | |
</body> | |
</html> |
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"code.google.com/p/go.net/websocket" | |
) | |
const MAX_CLIENT = 100 | |
type Client struct { | |
conn *websocket.Conn | |
userId string | |
} | |
var ( | |
chConn = make(chan Client, MAX_CLIENT) | |
chMsg = make(chan string, MAX_CLIENT) | |
) | |
func ChatServer(ws *websocket.Conn) { | |
buf := make([]byte, 1024) | |
n, _ := ws.Read(buf) | |
userId := string(buf[0:n]) | |
newClient := Client{ws, userId} | |
chConn <- newClient | |
chMsg <- userId + "님이 들어오셨습니다." | |
for n, e := ws.Read(buf); e == nil; n, e = ws.Read(buf) { | |
chMsg <- "<b>" + userId + "</b>: " + string(buf[0:n]) | |
} | |
chMsg <- userId + "님이 나가셨습니다." | |
ws.Close() | |
} | |
func listenAndServe() { | |
clients := make([]Client, 0, MAX_CLIENT) | |
for { | |
select { | |
case newClient := <-chConn: | |
clients = append(clients, newClient) | |
case msg := <-chMsg: | |
fmt.Println(string(msg)) | |
for _, v := range clients { | |
v.conn.Write([]byte(msg)) | |
} | |
} | |
} | |
} | |
func IndexPage(w http.ResponseWriter, r *http.Request) { | |
http.ServeFile(w, r, "./index.html") | |
} | |
func main() { | |
go listenAndServe() | |
http.HandleFunc("/", IndexPage) | |
http.Handle("/chat", websocket.Handler(ChatServer)) | |
if err := http.ListenAndServe(":9009", nil); err != nil { | |
panic("ListenAndServe: " + err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment