My favorites | Sign in
Project Home Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
REBOL [
Title: "Web Socket realtime chat demo"
Author: "Nenad Rakocevic/Softinnov"
Date: 01/10/2010
]

install-socket-app [
name: 'chat

users: make block! 10 ;-- stores [port! string!] pairs
history: make block! 50

broadcast: func [msg][
foreach port clients [send port msg] ;-- send same data to all connected users
]

on-connect: func [client][
foreach entry history [send client entry] ;-- send msgs' history to new user
foreach [port user] users [send client user] ;-- send connected users list
]

on-message: func [client data][
;-- escape all html tags for security concerns
data: copy data
replace/all data "<" "&lt;"
replace/all data ">" "&gt;"

switch data/1 [
#"m" [
insert next data join remold [now/time] " " ;-- insert [hh:mm:ss] time prefix
append history data
if 50 <= length? history [remove history] ;-- keep only 50 msgs in history
]
#"u" [
if not find users data [
repend users [client data] ;-- keep users list updated
]
]
]
broadcast data ;-- broadcast messages to all users
]

on-disconnect: func [client /local pos user][
pos: find users client
user: pos/2
remove/part pos 2
broadcast head change user #"r" ;-- send user quit msg to everyone
]
]

Change log

r52 by dockimbel on Jan 3, 2010   Diff
FEAT: web socket realtime chat demo added.
FEAT: changed socket application's SEND
function specs to SEND client [port!] data
[string!] (/with removed).
FIX:  timeout web socket issue clashing
with HTTP keepalive timeouts. Web socket
ports are no more timed out after 15sec.
Go to: 
Sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1335 bytes, 49 lines
Powered by Google Project Hosting