Chat server: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
Line 1,324:
// Start the server!
server = new ChatServer();</lang>
 
=={{header|Julia}}==
Modified to fit the Rosetta Code task from example code for the WebSockets module written by Leah Hanson.
To test, start the code and use a browser to connect to localhost:8000.
<lang julia>
using HttpServer
using WebSockets
 
#global Dict to store open connections in
global connections = Dict{Int,WebSocket}()
global usernames = Dict{Int,String}()
 
function decodeMessage( msg )
String(copy(msg))
end
 
 
wsh = WebSocketHandler() do req, client
global connections
@show connections[client.id] = client
println("req is $req")
notifyonline = "Connection from user number $(client.id) is now online."
for (k,v) in connections
if k != client.id
try
write(v, notifyonline)
catch
continue
end
end
end
while true
try
msg = read(client)
catch
telloffline = "User $(usernames[client.id]) disconnected."
println(telloffline, "(The client id was $(client.id).)")
delete!(connections, client.id)
if haskey(usernames, client.id)
delete!(usernames, client.id)
end
for (k,v) in connections
try
write(v, telloffline)
catch
continue
end
end
return
end
msg = decodeMessage(msg)
if startswith(msg, "setusername:")
println("SETTING USERNAME: $msg")
usernames[client.id] = msg[13:end]
notifyusername = "User number $(client.id) chose $(usernames[client.id]) as name handle."
for (k,v) in connections
try
write(v, notifyusername)
catch
println("Caught exception writing to user $k")
continue
end
end
end
if startswith(msg, "say:")
println("EMITTING MESSAGE: $msg")
for (k,v) in connections
if k != client.id
try
write(v, (usernames[client.id] * ": " * msg[5:end]))
catch
println("Caught exception writing to user $k")
continue
end
end
end
end
end
end
 
onepage = readstring(Pkg.dir("WebSockets","examples","chat-client.html"))
httph = HttpHandler() do req::Request, res::Response
Response(onepage)
end
 
server = Server(httph, wsh)
println("Chat server listening on 8000...")
run(server,8000)
</lang>
 
=={{header|Nim}}==
4,108

edits