Nenad Rakocevic - Softinnov 4-Jan-2010 13:04:55 GMT Article #20 Main page || Index || 2 Comments |
install-socket-app [
name: 'chat
;-- stores [port! string!] pairs
users: make block! 10
history: make block! 50
;-- send same data to all connected users
broadcast: func [msg][
foreach port clients [send port msg]
]
on-connect: func [client][
;-- send messages' history to new user
foreach entry history [send client entry]
;-- send connected users list
foreach [port user] users [send client user]
]
on-message: func [client data][
;-- escape all html tags for security concerns
data: copy data
replace/all data "<" "<"
replace/all data ">" ">"
switch data/1 [
#"m" [
;-- insert [hh:mm:ss] time prefix
insert next data join remold [now/time] " "
append history data
;-- keep only 50 messages in history
if 50 <= length? history [remove history]
]
#"u" [
if not find users data [
;-- keep users list updated
repend users [client data]
]
]
]
;-- broadcast messages to all users
broadcast data
]
on-disconnect: func [client /local pos user][
pos: find users client
user: pos/2
remove/part pos 2
;-- send user quit message to everyone
broadcast head change user #"r"
]
]