examples: annotate examples
[nit.git] / lib / websocket / examples / websocket_server.nit
index aa1124a..6b9e3b8 100644 (file)
 # limitations under the License.
 
 # Sample module for a minimal chat server using Websockets on port 8088
-module websocket_server
+module websocket_server is example
 
 import websocket
 
-var sock = new WebSocket(8088, 1)
+var sock = new WebSocketListener(8088, 1)
 
 var msg: String
 
-if sock.listener.eof then
+if sock.listener.closed then
        print sys.errno.strerror
 end
 
-sock.accept
+var cli: TCPStream
 
-while not sock.listener.eof do
-       if not sock.connected then sock.accept
-       if sys.stdin.poll_in then
-               msg = gets
-               printn "Received message : {msg}"
-               if msg == "exit" then sock.close
-               if msg == "disconnect" then sock.disconnect_client
-               sock.write(msg)
-       end
-       if sock.can_read(10) then
-               msg = sock.read_line
-               if msg != "" then print msg
+while not sock.closed do
+       cli = sock.accept
+       while cli.connected do
+               if sys.stdin.poll_in then
+                       msg = gets
+                       printn "Received message : {msg}"
+                       if msg == "disconnect" then cli.close
+                       cli.write(msg)
+               end
+               if cli.can_read(10) then
+                       msg = ""
+                       while cli.can_read(0) do msg += cli.read(100)
+                       if msg != "" then print msg
+               end
        end
 end