8000 Update mud example. · socketry/async-websocket@18c3a70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18c3a70

Browse files
committed
Update mud example.
1 parent 00a01a4 commit 18c3a70

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

examples/mud/client.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020
$stdout.write "> "
2121

2222
while line = $stdin.gets
23-
connection.write({input: line})
23+
connection.write(Protocol::WebSocket::TextMessage.generate({input: line}))
2424
connection.flush
2525

2626
$stdout.write "> "
2727
end
2828
end
2929

3030
while message = connection.read
31-
$stdout.puts message
31+
# Rewind to start of line:
32+
$stdout.write "\r"
33+
34+
# Clear line:
35+
$stdout.write "\e[2K"
36+
37+
# Print message:
38+
$stdout.puts message.to_h
39+
40+
$stdout.write "> "
3241
end
3342
end
3443
end

examples/mud/config.ru

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,28 @@ class User < Async::WebSocket::Connection
8383
@name || ANONYMOUS
8484
end
8585

86+
def send_message(value)
87+
self.write(Protocol::WebSocket::TextMessage.generate(value))
88+
end
89+
8690
def handle(message)
87-
key, *arguments = Command.split(message[:input])
91+
key, *arguments = Command.split(message.parse[:input])
8892
case key
8993
when "name"
9094
@name = arguments.first
9195
when "look"
92-
self.write({room: @room.as_json})
96+
self.send_message({room: @room.as_json})
9397
else
9498
if action = @room.actions[key]
9599
action.call(self, *arguments)
96100
else
97-
message[:user]
98101
@room.broadcast(message)
99102
end
100103
end
101104
end
102105

103106
def notify(text)
104-
self.write({notify: text})
107+
self.send_message({notify: text})
105108
self.flush
106109
end
107110

0 commit comments

Comments
 (0)
0