require "socket"
dts = TCPServer.open('127.0.0.1', 8080)
client=0
loop {
Thread.start(dts.accept) do |sock|
client = client+1
req=' <--- '
while line = sock.gets and line !~ /^\s*$/
# puts line
req << line
end
puts "req is " ,req
isget= req.index('GET')
if isget then
puts " is get "
body='<script>fetch("/data",{method:"post",body:JSON.stringify({"A":100})}).then( res=>(console.log(res) ))</script>'
else
puts " is post "
body= sock.recv(1024)
end
headers = ["HTTP/1.0 200 OK",
"Content-Type: text/html;charset=utf8",
"Content-Length: #{body.length}\r\n\r\n"].join("\r\n")
sock.write( headers+body)
sleep(1)
end
}
客户端用 浏览器 打开 http://localhost:8080/test