• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 5557224007

pending completion
5557224007

push

github

sirn-se
Close messages

57 of 57 new or added lines in 6 files covered. (100.0%)

605 of 607 relevant lines covered (99.67%)

23.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/src/Message/MessageHandler.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2023 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Message;
11

12
use Psr\Log\{
13
    LoggerInterface,
14
    LoggerAwareInterface,
15
    NullLogger
16
};
17
use WebSocket\BadOpcodeException;
18
use WebSocket\Frame\FrameHandler;
19

20
/**
21
 * WebSocket\Message\MessageHandler class.
22
 * Message/Frame handling.
23
 */
24
class MessageHandler implements LoggerAwareInterface
25
{
26
    private const DEFAULT_SIZE = 4096;
27

28
    private $frameHandler;
29
    private $logger;
30
    private $readBuffer;
31

32
    public function __construct(FrameHandler $frameHandler)
33
    {
34
        $this->frameHandler = $frameHandler;
76✔
35
        $this->setLogger(new NullLogger());
76✔
36
    }
37

38
    public function setLogger(LoggerInterface $logger): void
39
    {
40
        $this->logger = $logger;
76✔
41
        $this->frameHandler->setLogger($logger);
76✔
42
    }
43

44
    // Push message
45
    public function push(Message $message, bool $masked, int $size = self::DEFAULT_SIZE): void
46
    {
47
        $frames = $message->getFrames($size);
43✔
48
        foreach ($frames as $frame) {
43✔
49
            $this->frameHandler->push($frame, $masked);
43✔
50
        }
51
        $this->logger->info("[message-handler] Pushed {$message}", [
33✔
52
            'opcode' => $message->getOpcode(),
33✔
53
            'content-length' => $message->getLength(),
33✔
54
            'frames' => count($frames),
33✔
55
        ]);
33✔
56
    }
57

58
    // Pull message
59
    public function pull(): Message
60
    {
61
        do {
62
            $frame = $this->frameHandler->pull();
33✔
63
            $final = $frame->isFinal();
25✔
64
            $continuation = $frame->isContinuation();
25✔
65
            $opcode = $frame->getOpcode();
25✔
66
            $payload = $frame->getPayload();
25✔
67

68
            // Continuation and factual opcode
69
            $payload_opcode = $continuation ? $this->readBuffer['opcode'] : $opcode;
25✔
70

71
            // First continuation frame, create buffer
72
            if (!$final && !$continuation) {
25✔
73
                $this->readBuffer = ['opcode' => $opcode, 'payload' => $payload, 'frames' => 1];
7✔
74
                continue; // Continue reading
7✔
75
            }
76

77
            // Subsequent continuation frames, add to buffer
78
            if ($continuation) {
25✔
79
                $this->readBuffer['payload'] .= $payload;
6✔
80
                $this->readBuffer['frames']++;
6✔
81
            }
82
        } while (!$final);
25✔
83

84
        // Final, return payload
85
        $frames = 1;
25✔
86
        if ($continuation) {
25✔
87
            $payload = $this->readBuffer['payload'];
6✔
88
            $frames = $this->readBuffer['frames'];
6✔
89
            $this->readBuffer = null;
6✔
90
        }
91

92
        // Create message instance
93
        switch ($payload_opcode) {
94
            case 'text':
25✔
95
                $message = new Text();
19✔
96
                break;
19✔
97
            case 'binary':
15✔
98
                $message = new Binary();
1✔
99
                break;
1✔
100
            case 'ping':
15✔
101
                $message = new Ping();
4✔
102
                break;
4✔
103
            case 'pong':
14✔
104
                $message = new Pong();
7✔
105
                break;
7✔
106
            case 'close':
12✔
107
                $message = new Close();
12✔
108
                break;
12✔
109
            default:
110
                throw new BadOpcodeException("Invalid opcode '{$payload_opcode}' provided");
×
111
        }
112
        $message->setPayload($payload);
25✔
113

114
        $this->logger->info("[message-handler] Pulled {$message}", [
25✔
115
            'opcode' => $message->getOpcode(),
25✔
116
            'content-length' => $message->getLength(),
25✔
117
            'frames' => $frames,
25✔
118
        ]);
25✔
119

120
        return $message;
25✔
121
    }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc