UNPKG

1.66 kBPlain TextView Raw
1;; A simple chatserver written in LispyScript
2;; Connect to the server via telnet.
3;; $ telnet <host> <port>
4;; Any message typed in is broadcast to all other clients connected
5
6;; Include node TCP Library
7(var net (require "net"))
8
9;; Create TCP server
10(var chatServer (net.createServer))
11
12;; Set port to listen
13(var port 3000)
14
15;; Create array to store connected clients
16(var clientList [])
17
18;; Function to broadcast message to all the other
19;; connected clients
20(var broadcast
21 (function (message client)
22 ;; Loop through connected clients
23 (each clientList
24 (function (currentClient)
25 ;; Make sure you don't write to client that sent the message
26 (if (!= currentClient client)
27 (currentClient.write (str client.name " says " message)))))))
28
29;; The server connection event listener
30(chatServer.on "connection"
31 (function (client)
32 ;; set client.name to remote address + : + port
33 (set client.name (str client.remoteAddress ":" client.remotePort))
34 ;; Write Hi message to connected client
35 (client.write (str "Hi " client.name "\n"))
36 ;; Add client to client list
37 (clientList.push client)
38 ;; client data event listener, called whenever client sends
39 ;; some data
40 (client.on "data"
41 (function (data)
42 ;; call the broadcast function with data and client
43 (broadcast data client)))
44 ;; We dont want the server to crash while writing to a disconnected
45 ;; client. The 'end' event listener is called if client disconnects.
46 (client.on "end"
47 (function ()
48 (clientList.splice (clientList.indexOf client) 1)))))
49
50(chatServer.listen port)
51
\No newline at end of file