Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Conversation

@mabdulazim
Copy link
Contributor

update message attribute datatype to mixed rather than string to send json object as the data

update message attribute datatype to mixed rather than string to send json object as the data
@coveralls
Copy link

Coverage Status

Coverage remained the same at 68.125% when pulling 04ffc05 on mabdulazim:patch-1 into f3a50c7 on swooletw:master.

@albertcht
Copy link
Member

Hi @mabdulazim ,

A json encoded data should be string type as well. Can you show me more explicit example?

@mabdulazim
Copy link
Contributor Author

@albertcht the default parser receives the second parameter as a string or array or object.
if i send a json encoded, the parser considers it a normal text and the broadcast doesn’t fire

@mabdulazim
Copy link
Contributor Author

mabdulazim commented Apr 9, 2019

public function encode(string $event, $data)
{
    $packet = Packet::MESSAGE . Packet::EVENT;
    $shouldEncode = is_array($data) || is_object($data);
    $data = $shouldEncode ? json_encode($data) : $data;
    $format = $shouldEncode ? '["%s",%s]' : '["%s","%s"]';
    return $packet . sprintf($format, $event, $data);
}

by default this function receives $data as a string or array or object, so the $message attribute data type should not't be string
@albertcht

@rdrenth
Copy link

rdrenth commented Apr 10, 2019

In an earlier version it was possible that $message could be an array, which resulted the SocketIO payload parser to json_encode the message, but in more recent version it can only be a string or null.
And before the event gets pushed, the message (which has to be a string) gets passed to the SocketIO payload parser, which doesn't do anything cause it's already a string.

From the SwooleTW\Http\Concerns\InteractsWithWebsocket class:

public function pushMessage($server, array $data)
{
    $pusher = Pusher::make($data, $server);
    $pusher->push($this->payloadParser->encode(
        $pusher->getEvent(),
        $pusher->getMessage()
    ));
}

@albertcht IF this is how it's supposed to be, then you have to json_encode your websocket message manually before emitting it, but then the Websocket::emit(); function should also be strict and only allow a string as $data or am I wrong here?

@mabdulazim
Copy link
Contributor Author

@rdenth
when you encode the array or the object manually by json_encode() the event doesn’t work

@albertcht
Copy link
Member

albertcht commented Apr 10, 2019

Hi @mabdulazim and @rdrenth ,

Thanks for you clarification, in my original design for encode of payload parser, it expects mixed data type as second parameter. However, I neglected this part when I refactor Pusher.

Thanks for your pull request :)

@albertcht albertcht merged commit 44cf3b7 into swooletw:master Apr 10, 2019
@rdrenth
Copy link

rdrenth commented Apr 11, 2019

@mabdulazim Indeed, that didn't work, I was only assuming it would work without having it tested, thanks for your PR.
@albertcht Thanks for merging.

@mabdulazim mabdulazim deleted the patch-1 branch January 28, 2020 14:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants