websocket scheme

Started by Endo on 25-Jul-2010/17:06:32-7:00
Hi, here is my first web socket client implementation draft. First of all, it is very simple. I used this as an example: http://github.com/adamac/Java-WebSocket-client/blob/master/src/com/sixfire/websocket/WebSocket.java And I tested only with this simple websocket chat server: http://www.codeproject.com/KB/webservices/c_sharp_web_socket_server/WebSocketChatServer.zip run the chat server, it uses port 8181, so open that port on your firewall. start this script type; p: open url it should connect to the server and start the script again connect to the server. try 'insert p "hello"' from one client, on the other one, 'pick p 1' it should receive the message. currently no utf8 handling, no authentication support, no data packets handling. R E B O L [ -title: "Web Socket Client Protocol Implementation" -version: 0.0.1 -date: 2010-07-17 -author: "Semseddin (Endo) Moldibi" -license: 'public-domain ] make root-protocol [ -port-flags: system/standard/port-flags/pass-thru -open: func [port /local client-request tmp] [ --if port/port-id = 0 [port/port-id: 80] --client-request: rejoin [ ---"HTTP/1.1 101 Web Socket Protocol Handshake"-newline ---"Upgrade: WebSocket"-------newline ---"Connection: Upgrade"-------newline ---"WebSocket-Origin: " join http:// port/host--newline ---"WebSocket-Location: " port/url-----newline --] --port/locals: make object! [header: make string! 1024] --port/state/tail: 2000 --port/state/index: 0 --port/state/flags: port/state/flags or port-flags --;open communication port & send web socket request --open-proto/sub-protocol/generic port 'tcp --system/words/insert port/sub-port client-request --;read result --if not all [ ---equal? "HTTP/1.1 101 Web Socket Protocol Handshake"-first port/sub-port ---equal? "Upgrade: WebSocket"-------first port/sub-port ---equal? "Connection: Upgrade"------first port/sub-port --] [ ---print "Invalid handshake response." --] --set-modes port [lines: false no-wait: true] --;read headers --until [none? tmp: pick port 1] -] -insert: func [port data] [ --system/words/insert port/sub-port rejoin [#{00} data #{FF}] --data -] -;pick: func [port /local data b e] [ -pick: func [port] [ --data: make string! 63 --forever [ ---either error? try [ ----b: system/words/first port/sub-port ---] [ ----data: none ----break ---] [ ----if b > 0 [ -----if b = 255 [ ------break -----] -----append data to-char b ----] ---] --] --data -] -close: func [port][ --;close the connection cleanly --system/words/insert port/sub-port #{FF00} --wait 0.2 --system/words/close port/sub-port -] -net-utils/net-install ws self 0 ] print "protocol loaded" url: ws://localhost:8181/chat ;p: open url halt
here is the syntax highlighted version: http://www.moldibi.com/rebol/ws.html
I've updated the ws script. Now you can get the header just after connected to server. PICK returns empty string instead of none when no new message. http://www.moldibi.com/rebol/ws.html >> p: open url >> print p/locals/header WebSocket-Origin: http://localhost:8080 WebSocket-Location: ws://localhost:8181/chat >> pick p 1 == "" ;returns empty string if no new message >> insert p "test" == "test" >> pick p 1 == "me: test" ;returns the message string comes from server
Endo, I updated the rebolforum script to maintain your formatting :)

Reply