Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket closed with status code: 1005 () #1

Closed
Ahmed-Elbossily opened this issue Oct 10, 2021 · 7 comments
Closed

WebSocket closed with status code: 1005 () #1

Ahmed-Elbossily opened this issue Oct 10, 2021 · 7 comments

Comments

@Ahmed-Elbossily
Copy link

This error happens when I pick image from camera or gallery and the connection disconnected
Please help me to fix this issue.

@Dimolll
Copy link
Owner

Dimolll commented Oct 11, 2021

@Ahmed-Elbossily please provide me more info
Can you share the code where this is happening? What plugin do you use for pick image?

@Ahmed-Elbossily
Copy link
Author

Plugin i user for pick image : image_picker: ^0.8.4


This code for signalr methods:

Future connectUserSignalrPure({required User? user}) async {
try {
final builder = HubConnectionBuilder()
..url = Constants.baseURL + '/Chat'
..logLevel = LogLevel.information;
_connectionPure = builder.build();

  _connectionPure!.serverTimeoutInMilliseconds = 100000;
  _connectionPure!.keepAliveIntervalInMilliseconds = 100000;

  await _connectionPure!.startAsync();
  await _connectionPure!
      .invokeAsync('ConnectUser', [user!.username, user.uid]);
 
  _connectionPure!.on('onConnectUserError', (args) {
    print('User Connect Error: ${args.first.toString()}');
  });
} catch (e) {
  print(e);
}

}

Future sendFileFromUserPure(
{required Message message,
required String fileData,
required String fileExt,
required String fileName}) async {
MessageFile _message = MessageFile(
senderId: message.senderId,
receiverId: message.receiverId,
type: message.type,
photoUrl: message.photoUrl,
message: message.message,
timestamp: message.timestamp,
roomId: message.roomId,
sessionId: message.sessionId,
fileData: fileData,
fileExt: fileExt,
fileName: fileName,
newUser: message.newUser,
);

try {
  await _connectionPure!
      .invokeAsync('SendFileFromUser', [json.encode(_message)]);

  _connectionPure!.on('onSendFileFromUserError', (args) {
    print('Send File From User Error: ${args.first.toString()}');
  });
} catch (e) {
  print(e);
}

}


This code for signalr provider:

Future connectUserSignalrPure({required User? user}) async {
try {
await _userSignalrPure.connectUserSignalrPure(user: user);
notifyListeners();
} catch (e) {
print(e);
}
}

Future sendFileFromUserPure(
{required Message message,
required String fileData,
required String fileExt,
required String fileName}) async {
try {
await _userSignalrPure.sendFileFromUserPure(
message: message,
fileData: fileData,
fileExt: fileExt,
fileName: fileName,
);
notifyListeners();
} catch (e) {
print(e);
}
}


This code for pickImage in chatScreen method:

void pickImage({required ImageSource source}) async {
try {
File? selectedImage = await Utils.pickImage(source: source);
if (selectedImage != null) {
List imageFiles = [selectedImage];

    Uint8List data =
        await Utils.readFileByte(filePath: imageFiles.first.path);

    String byteString = base64.encode(data);

    Message _message = Message(
      senderId: widget.sender is User
          ? widget.sender.uid
          : widget.sender.employeeId.toInt().toString(),
      receiverId: widget.receiver is Employee
          ? widget.receiver.employeeId.toInt().toString()
          : widget.receiver.uid,
      message: p.extension(imageFiles.first.path).split('.')[1],
      type: Constants.messageTypeImage,
      timestamp: '',
      photoUrl: 'no',
      roomId: widget.roomId,
      sessionId: widget.sessionId,
      newUser: widget.sender is User ? widget.sender : widget.receiver,
    );

    if (widget.sender is User) {
      await userSignalrProvider!.sendFileFromUserPure(
        message: _message,
        fileData: byteString,
        fileExt: p.extension(imageFiles.first.path).split('.')[1],
        fileName: p.basenameWithoutExtension(imageFiles.first.path),
      );
    } else if (widget.sender is Employee) {
      await employeeSignalrProvider!.sendFileFromEmployeePure(
        message: _message,
        fileData: byteString,
        fileExt: p.extension(imageFiles.first.path).split('.')[1],
        fileName: p.basenameWithoutExtension(imageFiles.first.path),
      );
    }

    MessageStream.addNewMessage(message: _message);
  } else {
    return;
  }
} catch (e) {
  print(e);
  return;
}

}


This code for pickImage methods:

static Future<File?> pickImage({required ImageSource source}) async {
try {
XFile? selectedImage = await ImagePicker().pickImage(source: source);
File imageFile = File(selectedImage!.path);
if (imageFile != null) {
final bytes = imageFile.lengthSync();
final kb = bytes / 1024;
final mb = kb / 1024;
final extension = p.extension(imageFile.path);
if (extension.contains('jpeg') ||
extension.contains('jpg') ||
extension.contains('png')) {
if (mb < 4) {
return imageFile;
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
} catch (e) {
print(e);
return null;
}
}


@Dimolll
Copy link
Owner

Dimolll commented Oct 11, 2021

@Ahmed-Elbossily when error happened? And paste error more detail

@Ahmed-Elbossily
Copy link
Author

HttpConnection.stopConnection(Exception: WebSocket closed with status code: 1005 ().) called while in state ConnectionState.connected.
I/flutter (32495): Connection disconnected with error 'Exception: WebSocket closed with status code: 1005 ().'.
I/flutter (32495): HubConnection.connectionClosed(Exception: WebSocket closed with status code: 1005 ().) called while in state HubConnectionState.connected.
I/flutter (32495): HttpConnection.stopConnection(null) called while in state ConnectionState.disconnected.
I/flutter (32495): Call to HttpConnection.stopConnection(null) was ignored because the connection is already in the disconnected state.
I/flutter (32495): Exception: WebSocket closed with status code: 1005 ().

i open camer and pic image then send to server the server save the image correctly the next action is sending image url to the friend who i'm chat with then signalr closed

i see that the signalr closed only when i send file to server

code on server to save the image is iostream

@Dimolll
Copy link
Owner

Dimolll commented Oct 11, 2021

@Ahmed-Elbossily , problem in your sending data or on backend(when it parse your data). Problem not in this package.

You need to contact a backend representative and discuss this problem with him.

@Dimolll Dimolll closed this as completed Oct 11, 2021
@Ahmed-Elbossily
Copy link
Author

I make desctop client work well not closed
And also i make web client work well not close signalr

@Dimolll
Copy link
Owner

Dimolll commented Oct 11, 2021

@Ahmed-Elbossily problem may be in your sending data.
I see your issue in other signalR client jamiewest/signalr_core#69

You must check your sending data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants