c# 4.0 - How to send/receive messages through a web socket on windows phone 8 using the class ClientWebSocket? -
the web socket written in javascript colleague. managed connect. first of have log in on application using test account. have send email , password through json. have installed json.net packet using nuget.
some code found on reaserch this, not understand how send data using segment.
var buffer = new byte[1024]; var segment = new arraysegment<byte>(buffer); websocket.sendasync(segment, websocketmessagetype.text, true, cancellationtoken.none);
of course, can use object
user user=new user(); user.email="bla@bla.com"; user.password="pass"; string json = jsonconvert.serializeobject(user);
but not of use because method sendasync accepts byte type on segment.
all want send data, , if log in succeeds, should receive other data (in json format) user.
as side note, quite new web sockets, used http protocols asp.net web api 2.
i have no idea windows phone 8, code pasted seems similar regular .net clientwebsocket
, here have examples:
public static task sendstring(clientwebsocket ws, string data, cancellationtoken cancellation) { var encoded = encoding.utf8.getbytes(data); var buffer = new arraysegment<byte>(encoded, 0, encoded.length); return ws.sendasync(buffer, websocketmessagetype.text, true, cancellation); } public static async task<string> readstring(clientwebsocket ws) { arraysegment<byte> buffer = new arraysegment<byte>(new byte[8192]); websocketreceiveresult result = null; using (var ms = new memorystream()) { { result = await ws.receiveasync(buffer, cancellationtoken.none); ms.write(buffer.array, buffer.offset, result.count); } while (!result.endofmessage); ms.seek(0, seekorigin.begin); using (var reader = new streamreader(ms, encoding.utf8)) return reader.readtoend(); } }
if not compile or exists in wp8, find equivalent.
Comments
Post a Comment