Skip to content

Commit 762b7f4

Browse files
committed
Added --ip flag for 'twitch event websocket start'
1 parent 1a9c5a4 commit 762b7f4

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

cmd/events.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ var (
4545
clientId string
4646
version string
4747
websocketClient string
48+
websocketServerIP string
49+
websocketServerPort int
4850
)
4951

5052
// websocketCmd-specific flags
@@ -173,7 +175,8 @@ func init() {
173175

174176
// websocket flags
175177
/// flags for start-server
176-
websocketCmd.Flags().IntVarP(&port, "port", "p", 8080, "Defines the port that the mock EventSub websocket server will run on.")
178+
websocketCmd.Flags().StringVar(&websocketServerIP, "ip", "127.0.0.1", "Defines the ip that the mock EventSub websocket server will bind to.")
179+
websocketCmd.Flags().IntVarP(&websocketServerPort, "port", "p", 8080, "Defines the port that the mock EventSub websocket server will run on.")
177180
websocketCmd.Flags().BoolVar(&wsDebug, "debug", false, "Set on/off for debug messages for the EventSub WebSocket server.")
178181
websocketCmd.Flags().BoolVarP(&wsStrict, "require-subscription", "S", false, "Requires subscriptions for all events, and activates 10 second subscription requirement.")
179182

@@ -332,7 +335,7 @@ func websocketCmdRun(cmd *cobra.Command, args []string) {
332335

333336
if args[0] == "start-server" || args[0] == "start" {
334337
log.Printf("`Ctrl + C` to exit mock WebSocket servers.")
335-
mock_server.StartWebsocketServer(wsDebug, port, wsStrict)
338+
mock_server.StartWebsocketServer(wsDebug, websocketServerIP, websocketServerPort, wsStrict)
336339
} else {
337340
// Forward all other commands via RPC
338341
websocket.ForwardWebsocketCommand(args[0], websocket.WebsocketCommandParameters{

internal/events/websocket/mock_server/manager.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,20 @@ type ServerManager struct {
2424
serverList *util.List[WebSocketServer]
2525
reconnectTesting bool
2626
primaryServer string
27+
ip string
2728
port int
2829
debugEnabled bool
2930
strictMode bool
3031
}
3132

3233
var serverManager *ServerManager
3334

34-
func StartWebsocketServer(enableDebug bool, port int, strictMode bool) {
35+
func StartWebsocketServer(enableDebug bool, ip string, port int, strictMode bool) {
3536
serverManager = &ServerManager{
3637
serverList: &util.List[WebSocketServer]{
3738
Elements: make(map[string]*WebSocketServer),
3839
},
40+
ip: ip,
3941
port: port,
4042
reconnectTesting: false,
4143
strictMode: strictMode,
@@ -75,7 +77,7 @@ func StartWebsocketServer(enableDebug bool, port int, strictMode bool) {
7577
// Start HTTP server
7678
go func() {
7779
// Listen to port
78-
listen, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
80+
listen, err := net.Listen("tcp", fmt.Sprintf("%v:%v", ip, port))
7981
if err != nil {
8082
log.Fatalf("Cannot start HTTP server: %v", err)
8183
return
@@ -86,14 +88,14 @@ func StartWebsocketServer(enableDebug bool, port int, strictMode bool) {
8688
lightYellow := color.New(color.FgHiYellow).SprintFunc()
8789
yellow := color.New(color.FgYellow).SprintFunc()
8890

89-
log.Printf(lightBlue("Started WebSocket server on 127.0.0.1:%v"), port)
91+
log.Printf(lightBlue("Started WebSocket server on %v:%v"), ip, port)
9092
if serverManager.strictMode {
9193
log.Printf(lightBlue("--require-subscription enabled. Clients will have 10 seconds to subscribe before being disconnected."))
9294
}
9395

9496
fmt.Println()
9597

96-
log.Printf(yellow("Simulate subscribing to events at: http://127.0.0.1:%v/eventsub/subscriptions"), port)
98+
log.Printf(yellow("Simulate subscribing to events at: http://%v:%v/eventsub/subscriptions"), ip, port)
9799
log.Printf(yellow("POST, GET, and DELETE are supported"))
98100
log.Printf(yellow("For more info: https://dev.twitch.tv/docs/cli/websocket-event-command/#simulate-subscribing-to-mock-eventsub"))
99101

@@ -107,7 +109,7 @@ func StartWebsocketServer(enableDebug bool, port int, strictMode bool) {
107109
log.Printf(lightGreen("For further usage information, please see our official documentation:\nhttps://dev.twitch.tv/docs/cli/websocket-event-command/"))
108110
fmt.Println()
109111

110-
log.Printf(lightBlue("Connect to the WebSocket server at: ")+"ws://127.0.0.1:%v/ws", port)
112+
log.Printf(lightBlue("Connect to the WebSocket server at: ")+"ws://%v:%v/ws", ip, port)
111113

112114
// Serve HTTP server
113115
if err := http.Serve(listen, m); err != nil {
@@ -136,7 +138,7 @@ func StartWebsocketServer(enableDebug bool, port int, strictMode bool) {
136138
func wsPageHandler(w http.ResponseWriter, r *http.Request) {
137139
server, ok := serverManager.serverList.Get(serverManager.primaryServer)
138140
if !ok {
139-
log.Printf("Failed to find primary server [%v] when new client was accessing ws://127.0.0.1:%v/ws -- Aborting...", serverManager.primaryServer, serverManager.port)
141+
log.Printf("Failed to find primary server [%v] when new client was accessing ws://%v:%v/ws -- Aborting...", serverManager.primaryServer, serverManager.ip, serverManager.port)
140142
return
141143
}
142144

internal/events/websocket/mock_server/rpc_handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func RPCSubscriptionHandler(args rpc.RPCArgs) rpc.RPCResponse {
215215
return rpc.RPCResponse{
216216
ResponseCode: COMMAND_RESPONSE_MISSING_FLAG,
217217
DetailedInfo: "Command \"subscription\" requires flags --status, --subscription, and --session" +
218-
fmt.Sprintf("\nThe flag --subscription must be the ID of the subscription made at http://localhost:%v/eventsub/subscriptions", serverManager.port) +
218+
fmt.Sprintf("\nThe flag --subscription must be the ID of the subscription made at http://%v:%v/eventsub/subscriptions", serverManager.ip, serverManager.port) +
219219
"\nThe flag --status must be one of the non-webhook status options defined here:" +
220220
"\nhttps://dev.twitch.tv/docs/api/reference/#get-eventsub-subscriptions" +
221221
"\n\nExample: twitch event websocket subscription --status=user_removed --subscription=82a855-fae8-93bff0",

0 commit comments

Comments
 (0)