summaryrefslogtreecommitdiff
path: root/frontend/src/utils/websocket.js
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-13 22:18:56 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-13 22:18:56 +0200
commite7606b72118f71097a0a7f75fb735750f905c24a (patch)
tree6cf01487cf2c669e6231c2c16ea3d50093319b0b /frontend/src/utils/websocket.js
parentFix getPlayers that in fact takes an immutable List instead of JS array (diff)
downloadseven-wonders-e7606b72118f71097a0a7f75fb735750f905c24a.tar.gz
seven-wonders-e7606b72118f71097a0a7f75fb735750f905c24a.tar.bz2
seven-wonders-e7606b72118f71097a0a7f75fb735750f905c24a.zip
Migrate to seamless immutable
Resolves: https://github.com/luxons/seven-wonders/issues/6
Diffstat (limited to 'frontend/src/utils/websocket.js')
-rw-r--r--frontend/src/utils/websocket.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/frontend/src/utils/websocket.js b/frontend/src/utils/websocket.js
new file mode 100644
index 00000000..1dde5397
--- /dev/null
+++ b/frontend/src/utils/websocket.js
@@ -0,0 +1,23 @@
+import SockJS from 'sockjs-client'
+import Stomp from 'webstomp-client'
+import { eventChannel } from 'redux-saga'
+
+const wsURL = '/seven-wonders-websocket'
+
+export const createWsConnection = (headers = {}) => new Promise((resolve, reject) => {
+ let socket = Stomp.over(new SockJS(wsURL), {
+ debug: process.env.NODE_ENV !== "production"
+ })
+ socket.connect(headers, (frame) => {
+ return resolve({ frame, socket })
+ }, reject)
+})
+
+export const createSubscriptionChannel = (socket, path) => {
+ return eventChannel(emitter => {
+ const receiveUsernameHandler = socket.subscribe(path, event => {
+ emitter(JSON.parse(event.body))
+ })
+ return () => receiveUsernameHandler.unsubscribe()
+ })
+}
bgstack15