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

sirn-se / websocket-php / 5608975860

pending completion
5608975860

push

github

Sören Jensen
Middleware support

90 of 90 new or added lines in 8 files covered. (100.0%)

245 of 671 relevant lines covered (36.51%)

1.27 hits per line

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

77.36
/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;
5✔
35
        $this->setLogger(new NullLogger());
5✔
36
    }
37

38
    public function setLogger(LoggerInterface $logger): void
39
    {
40
        $this->logger = $logger;
5✔
41
        $this->frameHandler->setLogger($logger);
5✔
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);
4✔
48
        foreach ($frames as $frame) {
4✔
49
            $this->frameHandler->push($frame, $masked);
4✔
50
        }
51
        $this->logger->info("[message-handler] Pushed {$message}", [
4✔
52
            'opcode' => $message->getOpcode(),
4✔
53
            'content-length' => $message->getLength(),
4✔
54
            'frames' => count($frames),
4✔
55
        ]);
4✔
56
    }
57

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

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

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

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

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

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

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

120
        return $message;
4✔
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