@@ -403,19 +403,19 @@ func (c *Conn) writeControl(ctx context.Context, opcode opcode, p []byte) error
403
403
404
404
// MessageWriter returns a writer bounded by the context that will write
405
405
// a WebSocket data frame of type dataType to the connection.
406
- // Ensure you close the MessageWriter once you have written to entire message.
407
- // Concurrent calls to MessageWriter are ok.
408
- func (c * Conn ) MessageWriter (dataType DataType ) * MessageWriter {
409
- return & MessageWriter {
406
+ // Ensure you close the messageWriter once you have written to entire message.
407
+ // Concurrent calls to messageWriter are ok.
408
+ func (c * Conn ) MessageWriter (ctx context. Context , dataType DataType ) io. WriteCloser {
409
+ return & messageWriter {
410
410
c : c ,
411
- ctx : context . Background () ,
411
+ ctx : ctx ,
412
412
datatype : dataType ,
413
413
}
414
414
}
415
415
416
- // MessageWriter enables writing to a WebSocket connection.
417
- // Ensure you close the MessageWriter once you have written to entire message.
418
- type MessageWriter struct {
416
+ // messageWriter enables writing to a WebSocket connection.
417
+ // Ensure you close the messageWriter once you have written to entire message.
418
+ type messageWriter struct {
419
419
datatype DataType
420
420
ctx context.Context
421
421
c * Conn
@@ -429,7 +429,7 @@ type MessageWriter struct {
429
429
// The frame will automatically be fragmented as appropriate
430
430
// with the buffers obtained from http.Hijacker.
431
431
// Please ensure you call Close once you have written the full message.
432
- func (w * MessageWriter ) Write (p []byte ) (int , error ) {
432
+ func (w * messageWriter ) Write (p []byte ) (int , error ) {
433
433
if ! w .acquiredLock {
434
434
select {
435
435
case <- w .c .closed :
@@ -458,14 +458,9 @@ func (w *MessageWriter) Write(p []byte) (int, error) {
458
458
}
459
459
}
460
460
461
- // SetContext bounds the writer to the context.
462
- func (w * MessageWriter ) SetContext (ctx context.Context ) {
463
- w .ctx = ctx
464
- }
465
-
466
461
// Close flushes the frame to the connection.
467
- // This must be called for every MessageWriter .
468
- func (w * MessageWriter ) Close () error {
462
+ // This must be called for every messageWriter .
463
+ func (w * messageWriter ) Close () error {
469
464
if ! w .acquiredLock {
470
465
select {
471
466
case <- w .c .closed :
@@ -492,50 +487,35 @@ func (w *MessageWriter) Close() error {
492
487
// Please use SetContext on the reader to bound the read operation.
493
488
// Your application must keep reading messages for the Conn to automatically respond to ping
494
489
// and close frames.
495
- func (c * Conn ) ReadMessage (ctx context.Context ) (DataType , * MessageReader , error ) {
490
+ func (c * Conn ) ReadMessage (ctx context.Context ) (DataType , io. Reader , error ) {
496
491
select {
497
492
case <- c .closed :
498
493
return 0 , nil , xerrors .Errorf ("failed to read message: %w" , c .getCloseErr ())
499
494
case opcode := <- c .read :
500
- return DataType (opcode ), & MessageReader {
501
- ctx : context . Background () ,
495
+ return DataType (opcode ), & messageReader {
496
+ ctx : ctx ,
502
497
c : c ,
503
498
}, nil
504
499
case <- ctx .Done ():
505
500
return 0 , nil , xerrors .Errorf ("failed to read message: %w" , ctx .Err ())
506
501
}
507
502
}
508
503
509
- // MessageReader enables reading a data frame from the WebSocket connection.
510
- type MessageReader struct {
511
- n int
512
- limit int
513
- c * Conn
514
- ctx context.Context
504
+ // messageReader enables reading a data frame from the WebSocket connection.
505
+ type messageReader struct {
506
+ ctx context.Context
507
+ c * Conn
515
508
}
516
509
517
510
// SetContext bounds the read operation to the ctx.
518
511
// By default, the context is the one passed to conn.ReadMessage.
519
512
// You still almost always want a separate context for reading the message though.
520
- func (r * MessageReader ) SetContext (ctx context.Context ) {
513
+ func (r * messageReader ) SetContext (ctx context.Context ) {
521
514
r .ctx = ctx
522
515
}
523
516
524
- // Limit limits the number of bytes read by the reader.
525
- //
526
- // Why not use io.LimitReader? io.LimitReader returns a io.EOF
527
- // after the limit bytes which means its not possible to tell
528
- // whether the message has been read or a limit has been hit.
529
- // This results in unclear error and log messages.
530
- // This function will cause the connection to be closed if the limit is hit
531
- // with a close reason explaining the error and also an error
532
- // indicating the limit was hit.
533
- func (r * MessageReader ) Limit (bytes int ) {
534
- r .limit = bytes
535
- }
536
-
537
517
// Read reads as many bytes as possible into p.
538
- func (r * MessageReader ) Read (p []byte ) (n int , err error ) {
518
+ func (r * messageReader ) Read (p []byte ) (n int , err error ) {
539
519
select {
540
520
case <- r .c .closed :
541
521
return 0 , r .c .getCloseErr ()
@@ -546,11 +526,6 @@ func (r *MessageReader) Read(p []byte) (n int, err error) {
546
526
case <- r .c .closed :
547
527
return 0 , r .c .getCloseErr ()
548
528
case n := <- r .c .readDone :
549
- r .n += n
550
- // TODO make this better later and inside readLoop to prevent the read from actually occuring if over limit.
551
- if r .limit > 0 && r .n > r .limit {
552
- return 0 , xerrors .New ("message too big" )
553
- }
554
529
return n , nil
555
530
case <- r .ctx .Done ():
556
531
return 0 , r .ctx .Err ()
0 commit comments