@@ -47,8 +47,8 @@ import (
47
47
" context"
48
48
" fmt"
49
49
50
- " github.com/nitrictech/go-sdk/handler"
51
50
" github.com/nitrictech/go-sdk/nitric"
51
+ " github.com/nitrictech/go-sdk/nitric/websockets"
52
52
)
53
53
54
54
func main () {
@@ -67,34 +67,29 @@ func main() {
67
67
}
68
68
69
69
// Handle new WebSocket connections by storing the connection ID in the KV store.
70
- ws.On (handler. WebsocketConnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
70
+ ws.On (websockets. EventType_Connect , func (ctx *websockets. Ctx ) {
71
71
err := connections.Set (context.TODO (), ctx.Request .ConnectionID (), map [string ]interface {}{
72
72
" connectionId" : ctx.Request .ConnectionID (),
73
73
})
74
74
if err != nil {
75
- return ctx, err
75
+ return
76
76
}
77
-
78
- return next (ctx)
79
77
})
80
78
81
79
// Handle WebSocket disconnections by removing the connection ID from the KV store.
82
- ws.On (handler. WebsocketDisconnect , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
80
+ ws.On (websockets. EventType_Disconnect , func (ctx *websockets. Ctx ) {
83
81
err := connections.Delete (context.TODO (), ctx.Request .ConnectionID ())
84
82
if err != nil {
85
- return ctx, err
83
+ return
86
84
}
87
-
88
- return next (ctx)
89
85
})
90
86
91
87
// Handle incoming messages by broadcasting them to all other connections.
92
- ws.On (handler. WebsocketMessage , func (ctx *handler. WebsocketContext , next handler. WebsocketHandler ) (*handler. WebsocketContext , error ) {
88
+ ws.On (websockets. EventType_Message , func (ctx *websockets. Ctx ) {
93
89
connectionStream , err := connections.Keys (context.TODO ())
94
90
if err != nil {
95
- return ctx, err
91
+ return
96
92
}
97
-
98
93
senderId := ctx.Request .ConnectionID ()
99
94
100
95
for {
@@ -110,11 +105,9 @@ func main() {
110
105
message := fmt.Sprintf (" %s : %s " , senderId, ctx.Request .Message ())
111
106
err = ws.Send (context.TODO (), connectionId, []byte (message))
112
107
if err != nil {
113
- return ctx, err
108
+ return
114
109
}
115
110
}
116
-
117
- return next (ctx)
118
111
})
119
112
120
113
// Start the Nitric service to handle WebSocket events.
@@ -173,14 +166,14 @@ go get github.com/aws/aws-sdk-go/service/iam
173
166
174
167
### Create the Test File
175
168
176
- Create a new file named ` test_terraform_resources .go` in your project’s test directory:
169
+ Create a new file named ` terraform_resources_test .go` in your project’s test directory:
177
170
178
171
``` bash
179
172
mkdir -p test
180
- touch test/test_terraform_resources .go
173
+ touch test/terraform_resources_test .go
181
174
```
182
175
183
- Add the following code to ` test_terraform_resources .go` :
176
+ Add the following code to ` terraform_resources_test .go` :
184
177
185
178
``` go
186
179
package test
@@ -199,7 +192,7 @@ import (
199
192
func TestTerraformResources (t *testing .T ) {
200
193
// Set Terraform options, specifying the directory with your Terraform configuration
201
194
terraformOptions := &terraform.Options {
202
- TerraformDir: " ../cdktf.out/stacks/go-realtime -dev" ,
195
+ TerraformDir: " ../cdktf.out/stacks/my-websocket-app -dev" ,
203
196
}
204
197
205
198
// Ensure resources are destroyed after test completion
0 commit comments