c# - System.ObjectDisposedException throwed on WebSocket communication -
i'm trying communicate between web browser client , asp.net server using websockets.
i make set of requests, of different sizes , seconds of elapsed time between each of them. 3 first ones pass correctly, precise one, nothing in particular other, close websocket connection, throw exception on server side.
the error message , stack trace of exception :
fatal error: cannot access disposed object.
object name: 'system.web.websockets.aspnetwebsocket'.
@ system.web.websockets.aspnetwebsocket.throwifdisposed()
@ system.web.websockets.aspnetwebsocket.sendasyncimpl(arraysegment 1 buffer, websocketmessagetype messagetype, boolean endofmessage, cancellationtoken cancellationtoken, boolean performvalidation)
@ system.web.websockets.aspnetwebsocket.sendasync(arraysegment 1 buffer, websocketmessagetype messagetype, boolean endofmessage, cancellationtoken cancellationtoken)
@ [my code path here...]
it may threading problem, because i'm using async
methods everywhere functions communicate websockets.
i know exception throwed code (at socket.sendasync
):
public class sockettranslater { private websocket socket; private javascriptserializer serializer; // [...] private arraysegment<byte> encode(object data) { string json = serializer.serialize(data); return (new arraysegment<byte>(encoding.utf8.getbytes(json))); } public async task send(object packet) { arraysegment<byte> encoded = this.encode(packet); await socket.sendasync(encoded, websocketmessagetype.text, true, cancellationtoken.none); } }
the socket created class :
public class eventsender : ihttphandler { private static list<sockettranslater> sockettranslaters = new list<sockettranslater>(); public void processrequest(httpcontext context) { this.id = context.request.userhostname + ":" + context.request.useragent; if (context.iswebsocketrequest) { context.acceptwebsocketrequest(processsocket); } } private async task managesocket(aspnetwebsocketcontext context) { websocket socket = context.websocket; sockettranslater translater = new sockettranslater(socket); translaters.add(translater); while (true) { // manage messages, using other class send responses. translater.send(/* struct here */); } }
unhopefully, project big put code here.
any idea of error source, or additional information require ?
update:
after more tests, don't have exception time time. same thing, server seems have random comportment.
that makes problem weirder...
finally, after more tests , interesting questions , answer here (like this one), understood:
my problem was stocking websockets in dictionary linked hostnames. on first connection of client on server, worked correctly. if refresh page, websocket closed server (because there no chance use again) et 1 created.
but used same key both sockets, deprecated , new one, trying answer new client previous socket, closed. (disposed = closed asp.net).
so thing had remove socket list on client disconnection (at end of managesocket
). , forbid client connect twice same hostname.
i didn't mention part linking sockets hostnames, admit couldn't me... apologize.
Comments
Post a Comment