-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...es/streak/src/main/kotlin/de/codecentric/habitcentric/streak/messaging/WebSocketConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package de.codecentric.habitcentric.streak.messaging | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
//@EnableWebSocketSecurity | ||
class WebSocketConfig : WebSocketMessageBrokerConfigurer { | ||
override fun registerStompEndpoints(registry: StompEndpointRegistry) { | ||
// HTTP URL for WebSocket connection | ||
registry.addEndpoint("/streak-socket").setAllowedOrigins("*") | ||
} | ||
|
||
override fun configureMessageBroker(registry: MessageBrokerRegistry) { | ||
// register queue prefix meant for the application | ||
registry.setApplicationDestinationPrefixes("/app") | ||
|
||
// Configure spring internal broker to use /topic and /queue as prefix | ||
// There is no special meaning for the broker, it is more like a convention to differentiate | ||
// /topic as pub-sub and /queue as point to point messaging | ||
registry.enableSimpleBroker("/topic", "/queue") | ||
} | ||
|
||
// @Bean | ||
// fun configSecurity(messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<*>> { | ||
// messages.simpDestMatchers("/app").permitAll() | ||
// | ||
// return messages.build() | ||
// } | ||
} |
32 changes: 32 additions & 0 deletions
32
...treak/src/main/kotlin/de/codecentric/habitcentric/streak/messaging/WebSocketController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package de.codecentric.habitcentric.streak.messaging | ||
|
||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.handler.annotation.MessageMapping | ||
import org.springframework.messaging.handler.annotation.SendTo | ||
import org.springframework.messaging.simp.SimpMessagingTemplate | ||
import org.springframework.messaging.simp.annotation.SubscribeMapping | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Controller | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
@Controller | ||
class WebSocketController(val simpMessagingTemplate: SimpMessagingTemplate) { | ||
|
||
@MessageMapping("/helloApp") | ||
@SendTo("/topic/hello") | ||
fun appMessage(message: Message<String>): String { | ||
return message.payload | ||
} | ||
|
||
@SubscribeMapping("/ticker") | ||
fun sub(): String { | ||
return LocalDateTime.now().format(DateTimeFormatter.ISO_TIME); | ||
} | ||
|
||
@Scheduled(fixedDelay = 2000) | ||
fun emitTime() { | ||
val time = LocalDateTime.now().format(DateTimeFormatter.ISO_TIME) | ||
simpMessagingTemplate.convertAndSend("/topic/ticker", time); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useEffect, useState } from "react"; | ||
import { ActivationState, Client } from "@stomp/stompjs"; | ||
|
||
function Socket() { | ||
const [wsOutput, setWsOutput] = useState(""); | ||
|
||
useEffect(() => { | ||
// let accessToken = auth.user?.access_token || ""; | ||
// console.log(auth); | ||
// console.log("Token", accessToken); | ||
|
||
const client = new Client({ | ||
brokerURL: "ws://localhost:9005/streak-socket", | ||
// connectHeaders: { | ||
// Authentication: accessToken, | ||
// }, | ||
onWebSocketError: (e) => { | ||
console.log("ws error:", e); | ||
}, | ||
onWebSocketClose: (e) => { | ||
console.log("ws close:", e); | ||
}, | ||
debug: (str) => { | ||
console.log("debug:", str); | ||
}, | ||
onConnect: () => { | ||
client.subscribe("/app/ticker", (message) => | ||
console.log(`Received app ticker: ${message.body}`) | ||
); | ||
client.subscribe("/topic/ticker", (message) => | ||
console.log(`Received ticker: ${message.body}`) | ||
); | ||
client.subscribe("/topic/hello", (message) => | ||
console.log(`Received hello: ${message.body}`) | ||
); | ||
|
||
client.publish({ | ||
destination: "/app/helloApp", | ||
body: "Hi from stomp!", | ||
}); | ||
|
||
// client.publish({ destination: "/topic/test01", body: "First Message" }); | ||
}, | ||
onStompError: (frame) => { | ||
console.log("Broker reported error: " + frame.headers["message"]); | ||
console.log("Additional details: " + frame.body); | ||
}, | ||
onChangeState: (s) => { | ||
ActivationState; | ||
console.log("connection state: ", s); | ||
}, | ||
}); | ||
client.activate(); | ||
setWsOutput("hi!"); | ||
|
||
// client.subscribe("/topic", (message) => { | ||
// setWsOutput(message.body); | ||
// }); | ||
// | ||
return () => { | ||
console.trace(); | ||
console.log("destroy?"); | ||
client.deactivate(); | ||
}; | ||
}, []); | ||
|
||
return <pre>{wsOutput}</pre>; | ||
} | ||
|
||
export default Socket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters