ServerSentEvents specifies that a streaming endpoint should use the Server-Sent Events protocol for streaming instead of WebSockets. SSE is only suitable for server-to-client streaming. Methods using SSE typically use POST endpoints. When multiple SSE endpoints exist in a service, each should have
(args ...any)
| 83 | // }) |
| 84 | // }) |
| 85 | func ServerSentEvents(args ...any) { |
| 86 | if len(args) > 2 { |
| 87 | eval.TooManyArgError() |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | var fn func() |
| 92 | var dataField string |
| 93 | if len(args) > 0 { |
| 94 | switch actual := args[0].(type) { |
| 95 | case func(): |
| 96 | fn = actual |
| 97 | case string: |
| 98 | dataField = actual |
| 99 | case nil: |
| 100 | // Use the entire result as data field |
| 101 | default: |
| 102 | eval.InvalidArgError("function or string", args[0]) |
| 103 | return |
| 104 | } |
| 105 | if len(args) == 2 { |
| 106 | if fn != nil { |
| 107 | eval.TooManyArgError() |
| 108 | return |
| 109 | } |
| 110 | var ok bool |
| 111 | fn, ok = args[1].(func()) |
| 112 | if !ok { |
| 113 | eval.InvalidArgError("function", args[1]) |
| 114 | return |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | sse := &expr.HTTPSSEExpr{ |
| 120 | DataField: dataField, |
| 121 | } |
| 122 | |
| 123 | switch actual := eval.Current().(type) { |
| 124 | case *expr.HTTPExpr: |
| 125 | actual.SSE = sse |
| 126 | case *expr.HTTPServiceExpr: |
| 127 | actual.SSE = sse |
| 128 | case *expr.HTTPEndpointExpr: |
| 129 | actual.SSE = sse |
| 130 | case *expr.JSONRPCExpr: |
| 131 | actual.SSE = sse |
| 132 | default: |
| 133 | eval.IncompatibleDSL() |
| 134 | } |
| 135 | |
| 136 | if fn != nil { |
| 137 | eval.Execute(fn, sse) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // SSERequestID defines the attribute of the Payload type that provides the |
| 142 | // Last-Event-ID request header value. The attribute must exist in the Payload |
no test coverage detected