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

String/Bytes Type Mismatch in WebSocket Communication #335

Open
dnhrdt opened this issue Feb 13, 2025 · 1 comment
Open

String/Bytes Type Mismatch in WebSocket Communication #335

dnhrdt opened this issue Feb 13, 2025 · 1 comment

Comments

@dnhrdt
Copy link

dnhrdt commented Feb 13, 2025

There is a type mismatch between the server's expected data type and the client's sent data type when handling the end-of-audio signal.

Issue

The server's get_audio_from_websocket() method expects binary data and checks for b"END_OF_AUDIO":

def get_audio_from_websocket(self, websocket):
    frame_data = websocket.recv()
    if frame_data == b"END_OF_AUDIO":  # Expects bytes
        return False
    return np.frombuffer(frame_data, dtype=np.float32)

However, the ServeClientBase.disconnect() method sends a string message:

def disconnect(self):
    self.websocket.send(json.dumps({  # Sends string
        "uid": self.client_uid,
        "message": self.DISCONNECT
    }))

This causes a "bytes-like object is required, not 'str'" error when the server tries to process the disconnect message.

Steps to Reproduce

  1. Start WhisperLive server
  2. Connect client
  3. Send audio data
  4. Try to disconnect client
  5. Observe error in server logs: "Unexpected error: a bytes-like object is required, not 'str'"

Expected Behavior

The server should either:

  1. Accept string messages for control signals like disconnect
  2. Or document that all WebSocket communication must be binary data

Suggested Fix

Either:

Modify disconnect() to send binary data:

def disconnect(self):
    self.websocket.send(b"END_OF_AUDIO")

Or modify get_audio_from_websocket() to handle both binary and string data:

def get_audio_from_websocket(self, websocket):
    frame_data = websocket.recv()
    if isinstance(frame_data, str):
        try:
            control_msg = json.loads(frame_data)
            if control_msg.get("message") == self.DISCONNECT:
                return False
        except json.JSONDecodeError:
            pass
    elif frame_data == b"END_OF_AUDIO":
        return False
    return np.frombuffer(frame_data, dtype=np.float32)
@makaveli10
Copy link
Collaborator

@dnhrdt Thanks for raising the issue. Feel free to open a PR with the fix.

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

No branches or pull requests

2 participants