Home > Net >  WebSocket is unusual, to help find where is the exception occurs, a few lines of code, I found two d
WebSocket is unusual, to help find where is the exception occurs, a few lines of code, I found two d

Time:01-11

Caller:
 try {socket. Send (strMsg); }
Catch Exception (err) {
//the err here is "the object reference not set to an instance of an object,"
}


Classes encapsulate code:
 public async Task Send (string userMsg) {
//monitor wsCtx IsClientConnected is true, not false so wsCtx wasn't released state
if(! WsCtx. IsClientConnected) {OnMessage (" Lose ", 0); return; }
//here if don't have to try, the caller may appear "error index exceeded array bounds", added the try is "the object reference not set to an instance of an object,"
Try {var buffer=new ArraySegment (Encoding UTF8. GetBytes (userMsg));
Await wsCtx. WebSocket. SendAsync (buffer, WebSocketMessageType. Text, true, CancellationToken. None).
} catch {}
}

Private void OnMessage (string MSG, int code) {
Try {MessageBack. Invoke (MSG, code); } catch {}
}

CodePudding user response:

Socket is surely not release object, because the exception occurs, the monitoring socket. WsCtx connection state, true or false

CodePudding user response:

To solve the problem is that classes encapsulate the inside with the try, why there will be abnormal ran to the caller, where the exception happened,

CodePudding user response:

Understand probably try catch mechanism, if there is no catch specific types of errors, then the external if use the try catch, is still can catch the wrong,
If internal have a try, the outside to try again, then after catch exceptions to interrupt the current processing; If you add external no longer try, so there are abnormal that line will continue in the future,

CodePudding user response:

Error to grab from fountainhead

CodePudding user response:

For the same program, don't always want to eat "exception", the program Debug and Release versions, the Debug version is to let the bugs as soon as possible, to be captured in the debugger application exception thrown to the outermost layer of the program, unified dialog box encapsulation and hint to the user, you are using a try... Catch code not only deprive yourself of the debug technology ability, and further make the system you run "poisoned", cause happening in the subsequent "puzzling" bugs, in fact is you try... Catch would like to hide their code to the caller, instead of bugs, to treat this kind of problem, the right thing to do is to remove the try... The catch, find bugs as soon as possible, and find the problem of your code, serious rather than using a try... Catch statement to "hide bugs" cheat the caller (in fact, the subsequent bugs are you made another).

CodePudding user response:

. I also think so, but await wsCtx WebSocket. SendAsync this method always quote "the object reference not set to an instance of an object," exception, confusing,
WsCtx IsClientConnected is true, wsCtx. WebSocket. The State is Open, still can throw this exception, I can do,

CodePudding user response:

Then, don't VS, part c # I was written in notepad, just a few lines of code, compiled into a DLL for javascript call, main business processing in javascript,
Normally not exception, is 20 to personal attachment, every 1 minute or so out of such a abnormal, sometimes luck, half an hour,
  • Related