我正在尝试在瘦服务器上使用 websocket。以下代码尝试运行一个时钟,该时钟每 0.1 秒更新一次网页上的时间。
PAGE
是要呈现的初始页面的内容。- 瘦服务器使用
serve
和session
方法启动。 - Websocket由
websocket
方法发起。 tick_every
是一个实用函数,它在给定的每个时间间隔的正确时间调用一个块。
代码:
require "rack"
require "thin"
require "em-websocket"
PAGE = <<_
<html>
<body><div id="output"></div></body>
<script>
window.addEventListener("load", init, false);
function init(){
websocket = new WebSocket("ws://localhost:4000");
websocket.onmessage = function(evt){
document.getElementById("output").innerHTML = evt.data;
};
}
</script>
<div id="output"></div>
</html>
_
def serve
Rack::Handler::Thin.run(Rack::Builder.new do
map("/"){run(->env{session(env)})}
end, Port: 3000)
end
def session env
websocket(env["SERVER_NAME"])
[200, {}, [PAGE]]
end
def websocket host
EM.run do
EM::WebSocket.run(host: host, port: 4000) do |ws|
ws.onopen do
tick_every(0.1){|t| ws.send "The time now since epoch in sec is #{t}"}
end
end
end
end
def tick_every sec, &pr
Thread.new do loop do
t = Time.now.to_f # present time in micro seconds
frac = t.modulo(sec.to_f) # fractional (decimal) part of it w.r.t. `sec`
pr.call((t - frac).round(6)) # calls the block, passing the present time with precision of `sec`
sleep(sec - frac) # wait for the next flat `sec`
end end
end
serve
当我运行它并在 处打开网页时localhost:3000
,websocket
在控制台中返回一条错误消息:
!! Unexpected error while processing request: no acceptor (port is in use or requires root privileges)
并在网页上显示初始时间,但三十秒后不更新(不确定,但这似乎是恒定的,可能有一些含义),之后开始每 0.1 秒更新时钟。
- 是什么导致来自 websocket 的错误消息?
- 为什么它会暂停三十秒然后开始工作?
- 这是结合ajax和websocket的正确方法吗?
- 我该如何解决这些问题?