这是Ruby对WebSocket的实现,示例代码:
#Runstheserveratport10081.Itallowsconnectionswhoseoriginisexample.com.server=WebSocketServer.new(:port=>10081,:accepted_domains=>["example.com"])server.run()do|ws|#Theblockiscalledforeachconnection.#Checksrequestedpath.ifws.path=="/"#Callws.handshake()withoutargumentfirst.ws.handshake()#ReceivesonemessagefromtheclientasString.whiledata=ws.receive()puts(data)#Sendsthemessagetotheclient.ws.send(data)endelse#Youcancallws.handshake()withargumenttoreturnerrorstatus.ws.handshake("404NotFound")endend
评论