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

sirn-se / websocket-php / 5610959835

pending completion
5610959835

push

github

Sören Jensen
Middleware support

14 of 14 new or added lines in 4 files covered. (100.0%)

283 of 676 relevant lines covered (41.86%)

1.66 hits per line

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

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

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

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

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

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

71
            // First continuation frame, create buffer
72
            if (!$final && !$continuation) {
1✔
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) {
1✔
79
                $this->readBuffer['payload'] .= $payload;
×
80
                $this->readBuffer['frames']++;
×
81
            }
82
        } while (!$final);
1✔
83

84
        // Final, return payload
85
        $frames = 1;
1✔
86
        if ($continuation) {
1✔
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':
1✔
95
                $message = new Text();
1✔
96
                break;
1✔
97
            case 'binary':
×
98
                $message = new Binary();
×
99
                break;
×
100
            case 'ping':
×
101
                $message = new Ping();
×
102
                break;
×
103
            case 'pong':
×
104
                $message = new Pong();
×
105
                break;
×
106
            case 'close':
×
107
                $message = new Close();
×
108
                break;
×
109
            default:
110
                throw new BadOpcodeException("Invalid opcode '{$payload_opcode}' provided");
×
111
        }
112
        $message->setPayload($payload);
1✔
113

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

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