Skip to content

Commit 7a49c0e

Browse files
feat: create signalR client
1 parent bcec1a2 commit 7a49c0e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2023. Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package infrastructure.signalr
10+
11+
import application.presenter.EventConsumer
12+
import application.presenter.EventParser
13+
import com.microsoft.signalr.HubConnectionBuilder
14+
import entities.events.Event
15+
import infrastructure.digitaltwins.DTEventParser
16+
import io.reactivex.rxjava3.core.FlowableEmitter
17+
18+
/**
19+
* The consumer of events from a SignalR topic.
20+
*/
21+
class SignalRClient : EventConsumer<String> {
22+
23+
init {
24+
requireNotNull(System.getenv("SIGNALR_CONNECTION_STRING")) {
25+
println("Invalid connection String: please provide a valid connection String!")
26+
}
27+
}
28+
29+
private val connection = HubConnectionBuilder.create(System.getenv("SIGNALR_CONNECTION_STRING")).build()
30+
private val eventParser: EventParser<String> = DTEventParser()
31+
32+
override fun start(emitter: FlowableEmitter<Event<Any>>) {
33+
connection.on("newMessage", { event ->
34+
emitter.onNext(consumeEvent(event))
35+
}, String::class.java)
36+
connection.start()
37+
}
38+
39+
override fun consumeEvent(inputEvent: String): Event<Any> = eventParser.parseEvent(inputEvent)
40+
41+
}

0 commit comments

Comments
 (0)