Created
September 9, 2011 07:21
-
-
Save uiur/1205663 to your computer and use it in GitHub Desktop.
Simple Chat in Ruby+WebSocket
This file contains hidden or 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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<title>chat</title> | |
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js'></script> | |
<script src='websock.js'></script> | |
</head> | |
<body onload='init();'> | |
<input id='msg' type='text' /> | |
<div id='debug'> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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
# encoding: UTF-8 | |
require 'em-websocket' | |
class User | |
@@users = [] | |
attr_accessor :socket, :name | |
class << self | |
def all | |
@@users | |
end | |
def size | |
@@users.size | |
end | |
def include?(socket) | |
@@users.map{|u| u.socket}.include?(socket) | |
end | |
def find_by_socket(socket) | |
@@users[@@users.map{|u| u.socket}.index(socket)] | |
end | |
def delete(socket) | |
@@users.delete_if{|user| user.socket == socket} | |
end | |
def send_to_all(msg) | |
@@users.each do |user| | |
user.socket.send(msg) | |
end | |
end | |
end | |
def initialize(socket, name=nil) | |
names = ['純一郎','晋三','康夫','太郎','由紀夫','直人','佳彦'] | |
@socket = socket | |
@name = name || names.sample | |
@@users << self unless User.include?(socket) | |
end | |
end | |
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| | |
ws.onopen do | |
connect_user = User.new(ws) | |
ws.send "Connected. #{User.size} clients here." | |
ws.send "Your name is #{connect_user.name}" | |
users = User.all | |
users.each do |user| | |
user.socket.send(connect_user.name + "がインしたお") unless user.socket == connect_user.socket | |
end | |
end | |
ws.onmessage do |msg| | |
if msg =~ /\/nick (.*)/ | |
send_user = User.find_by_socket(ws) | |
prev_name = send_user.name | |
send_user.name = $1 | |
User.send_to_all(prev_name + 'は名前を' + send_user.name + 'に変更しました') | |
else | |
send_user = User.find_by_socket(ws) | |
users = User.all | |
users.each do |user| | |
user.socket.send(send_user.name + ": " + msg) | |
end | |
end | |
end | |
ws.onclose do | |
User.delete(ws) | |
puts "WebSocket closed" | |
end | |
ws.onerror { |e| puts "Error: #{e.message}" } | |
end |
This file contains hidden or 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
function init () { | |
function show(string) { | |
var element = document.getElementById("debug"); | |
var p = document.createElement("p"); | |
p.appendChild(document.createTextNode(string)); | |
element.appendChild(p); | |
} | |
var ws = new WebSocket('ws://localhost:8080/'); | |
ws.onopen = function(event) { | |
}; | |
ws.onmessage = function (event) { | |
show(event.data); | |
}; | |
$("#msg").keypress(function(e){ | |
if(e.keyCode == 13){ | |
var val = $("#msg").val(); | |
if(val !== '') { | |
ws.send($("#msg").val()); | |
$("#msg").val(""); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment