You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

message_writer.go 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package controllers
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/astaxie/beego/context"
  19. )
  20. type RefinedWriter struct {
  21. context.Response
  22. writerCleaner Cleaner
  23. buf []byte
  24. }
  25. func newRefinedWriter(w context.Response) *RefinedWriter {
  26. return &RefinedWriter{w, *NewCleaner(6), []byte{}}
  27. }
  28. func (w *RefinedWriter) Write(p []byte) (n int, err error) {
  29. data := strings.TrimRight(strings.TrimLeft(string(p), "event: message\ndata: "), "\n\n")
  30. if w.writerCleaner.cleaned == false && w.writerCleaner.dataTimes < w.writerCleaner.bufferSize {
  31. w.writerCleaner.AddData(data)
  32. if w.writerCleaner.dataTimes == w.writerCleaner.bufferSize {
  33. cleanedData := w.writerCleaner.GetCleanedData()
  34. w.buf = append(w.buf, []byte(cleanedData)...)
  35. fmt.Print(cleanedData)
  36. return w.ResponseWriter.Write([]byte(fmt.Sprintf("event: message\ndata: %s\n\n", cleanedData)))
  37. }
  38. return 0, nil
  39. }
  40. w.buf = append(w.buf, []byte(data)...)
  41. fmt.Print(data)
  42. return w.ResponseWriter.Write(p)
  43. }
  44. func (w *RefinedWriter) String() string {
  45. return string(w.buf)
  46. }
  47. type Cleaner struct {
  48. dataTimes int // Number of times data is added
  49. buffer []string // Buffer of tokens
  50. bufferSize int // Size of the buffer
  51. cleaned bool // Whether the data has been cleaned
  52. }
  53. func NewCleaner(bufferSize int) *Cleaner {
  54. return &Cleaner{
  55. dataTimes: 0,
  56. buffer: make([]string, 0, bufferSize),
  57. bufferSize: bufferSize,
  58. cleaned: false,
  59. }
  60. }
  61. func (c *Cleaner) AddData(data string) {
  62. c.buffer = append(c.buffer, data)
  63. c.dataTimes++
  64. }
  65. func (c *Cleaner) GetCleanedData() string {
  66. c.cleaned = true
  67. return cleanString(strings.Join(c.buffer, ""))
  68. }
  69. func cleanString(data string) string {
  70. data = strings.Replace(data, "?", "", -1)
  71. data = strings.Replace(data, "?", "", -1)
  72. data = strings.Replace(data, "-", "", -1)
  73. data = strings.Replace(data, "——", "", -1)
  74. if strings.Contains(data, ":") {
  75. parts := strings.Split(data, ":")
  76. data = parts[len(parts)-1]
  77. } else if strings.Contains(data, ":") {
  78. parts := strings.Split(data, ":")
  79. data = parts[len(parts)-1]
  80. }
  81. data = strings.TrimSpace(data)
  82. return data
  83. }