HTML5 Web Socket 服务器/客户端的Ruby实现 - web-socket-ruby
这是一个采用Ruby实现的HTML5 Web Socket 服务器/客户端。
* 如何运行示例 - Run sample Web Socket server (echo server) with: $ ruby samples/echo_server.rb localhost 10081 - Run sample Web Socket client and type something: $ ruby samples/stdio_client.rb ws://localhost:10081 Ready hoge Sent: "hoge" Received: "hoge" * 使用示例 服务器: # Runs the server at port 10081. It allows connections whose origin is example.com. server = WebSocketServer.new(:port => 10081, :accepted_domains => ["example.com"]) server.run() do |ws| # The block is called for each connection. # Checks requested path. if ws.path == "/" # Call ws.handshake() without argument first. ws.handshake() # Receives one message from the client as String. while data = ws.receive() puts(data) # Sends the message to the client. ws.send(data) end else # You can call ws.handshake() with argument to return error status. ws.handshake("404 Not Found") end end 客户端: # Connects to Web Socket server at host example.com port 10081. client = WebSocket.new("ws://example.com:10081/") # Sends a message to the server. client.send("Hello") # Receives a message from the server. data = client.receive() puts(data) * 提示: JavaScript 客户端实现 Google Chrome Dev Channel 原生支持 Web Socket. 对于其它浏览器,你可以使用一个Flash实现代替:http://github.com/gimite/web-socket-js/tree/master
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!