c# 4.0 - C# - Websocket - Sending Message Back To Client -
i've been working on c# web socket server 24 hours.
i've figured out how complete handshake , connection initialized.
also i've figured out how take byte[]
data , decode it's original string.
but i'm stuck , looking help.
i can't seem figure out how put proper data structure , send client. if send original data received websocket on client-side tell data cannot masked (which why needs decoded).
so basically, i'm asking in short how structure response data send websocket client?
i've been using http://tools.ietf.org/html/rfc6455 resource research.
please keep in mind i'm using regular socket this.
here decode code:
if (databuffer.length > 0) { if (databuffer[0] == 129) { int msg_length = databuffer[1] - 128; if (msg_length <= 125) { // msg ready decode. log.info("message length: " + msg_length); byte[] decoded = new byte[databuffer.length]; byte[] encoded = new byte[databuffer.length - 6]; array.copy(databuffer, 6, encoded, 0, msg_length); byte[] key = new byte[4] { databuffer[2], databuffer[3], databuffer[4], databuffer[5] }; (int = 0; < encoded.length; i++) { decoded[i] = (byte)(encoded[i] ^ key[i % 4]); } log.info("msg: " + encoding.utf8.getstring(decoded)); byte[] return_msg = new byte[decoded.length + 8]; return_msg[0] = 1; return_msg[1] = 0; return_msg[2] = 0; return_msg[3] = 0; // op code return_msg[4] = 0x1; return_msg[5] = 0x0; return_msg[6] = 0x0; return_msg[7] = 0x0; array.copy(decoded, 0, return_msg, 8, decoded.length); socket.send(return_msg); } else if (msg_length == 126) { // longer message msg_length = databuffer[2] + databuffer[3]; log.info("message length: " + msg_length); byte[] key = new byte[4] { databuffer[4], databuffer[5], databuffer[6], databuffer[7] }; byte[] decoded = new byte[databuffer.length]; byte[] encoded = new byte[databuffer.length - 8]; array.copy(databuffer, 8, encoded, 0, msg_length); (int = 0; < encoded.length; i++) { decoded[i] = (byte)(encoded[i] ^ key[i % 4]); } log.info("msg: " + encoding.utf8.getstring(decoded)); byte[] return_msg = new byte[decoded.length + 4]; return_msg[0] = 129; return_msg[1] = 0; return_msg[2] = 0; return_msg[3] = 0; array.copy(decoded,0,return_msg,4,decoded.length); socket.send(return_msg); } else if (msg_length == 127) { // huge message: log.info("big message"); } } }
take 2 articles writing c# websocket servers:
https://developer.mozilla.org/en-us/docs/websockets/writing_websocket_servers
https://developer.mozilla.org/en-us/docs/websockets/writing_websocket_server
it seems both same article, not! in this part of first link have explanation how build frames.
update
the first byte contains several information:
- fin :indicates frame contains complete message.
- rsv1: option 1
- rsv2: option 2
- rsv3: option 3
- optcode: indicates type of frame.
if want send text message smaller 125 bytes, lets message has 90 bytes, in first byte put bit 0 1(more significant), next 3 0, unless want enable options, , next 4 0001, indicating text frame. first bye 10000001, or 129.
now in second byte, first bit indicates if frame masked. don't mask frames server client, set 0. next 7 bits indicates rather length, or type of frame length. because sending small frame, can indicate value 125 in 7 bits. because frame length 90 bytes, second byte of header 01011010, or 90.
so when sending server client text frame of 90 byes, first 2 bytes conforming header 129 , 90. rest of message 90 bytes of utf8 encoded bytes.
if frame longer 125 bytes, length of header, check spec. if frame need masked (like frames client), first 4 bytes of body contain masking key. see there tidbits cope with, recommend read spec : https://tools.ietf.org/html/rfc6455
Comments
Post a Comment