Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>
-var sock = new WebSocket(8088, 1)
+var sock = new WebSocketListener(8088, 1)
print sys.errno.strerror
end
print sys.errno.strerror
end
-while not sock.listener.closed 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 = ""
- while sock.can_read(0) do msg += sock.read(100)
- 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
intrude import standard::stream
intrude import standard::stream
-# Websocket compatible server, works as an extra layer to the original Sockets
-class WebSocket
- super BufferedIStream
- super OStream
- super PollableIStream
-
- # Client connection to the server
- var client: TCPStream
+# Websocket compatible listener
+#
+# Produces Websocket client-server connections
+class WebSocketListener
+ super Socket
# Socket listening to connections on a defined port
var listener: TCPServer
# Socket listening to connections on a defined port
var listener: TCPServer
# Creates a new Websocket server listening on given port with `max_clients` slots available
init(port: Int, max_clients: Int)
do
# Creates a new Websocket server listening on given port with `max_clients` slots available
init(port: Int, max_clients: Int)
do
- _buffer = new FlatBuffer
- _buffer_pos = 0
listener = new TCPServer(port)
listener.listen max_clients
end
listener = new TCPServer(port)
listener.listen max_clients
end
- # Accept an incoming connection and initializes the handshake
- fun accept
+ # Accepts an incoming connection
+ fun accept: WebsocketConnection
do
assert not listener.closed
var client = listener.accept
assert client != null
do
assert not listener.closed
var client = listener.accept
assert client != null
+ return new WebsocketConnection(listener.port, "", client)
+ end
+
+ # Stop listening for incoming connections
+ fun close
+ do
+ listener.close
+ end
+end
+
+# Connection to a websocket client
+#
+# Can be used to communicate with a client
+class WebsocketConnection
+ super TCPStream
+
+ init do
+ _buffer = new FlatBuffer
+ _buffer_pos = 0
var headers = parse_handshake
var resp = handshake_response(headers)
client.write(resp)
end
var headers = parse_handshake
var resp = handshake_response(headers)
client.write(resp)
end
- # Disconnect from a client
- fun disconnect_client
- do
- client.close
- end
+ # Client connection to the server
+ var client: TCPStream
- # Disconnects the client if one is connected
- # And stops the server
+ # Disconnect from a client
redef fun close
do
client.close
redef fun close
do
client.close
end
# Parses the input handshake sent by the client
end
# Parses the input handshake sent by the client
do
buf.append client.read_line
buf.append("\r\n")
do
buf.append client.read_line
buf.append("\r\n")
- if buf.has_substring("\r\n\r\n", buf.length - 4) then return buf.to_s
+ if buf.has_suffix("\r\n\r\n") then return buf.to_s
return read_http_frame(buf)
end
return read_http_frame(buf)
end
end
# Checks if a connection to a client is available
end
# Checks if a connection to a client is available
- fun connected: Bool do return client.connected
+ redef fun connected do return client.connected
- redef fun write(msg: Text)
do
client.write(frame_message(msg.to_s))
end
do
client.write(frame_message(msg.to_s))
end