-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Callhub-Connect/setup-websocket
Set up two way websocket connection
- Loading branch information
Showing
5 changed files
with
55 additions
and
45 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,47 @@ | ||
|
||
import { Client } from '@stomp/stompjs'; | ||
import websocket from 'websocket'; | ||
|
||
Object.assign(global, { WebSocket: websocket.w3cwebsocket }) | ||
|
||
const client = new Client({ | ||
brokerURL: 'ws://localhost:8080/mywebsockets', | ||
onConnect: () => { | ||
client.subscribe('/topic/messages', message => | ||
console.log(`Received: ${message.body}`) | ||
); | ||
|
||
client.onWebSocketError = (error) => { | ||
console.error('Error with websocket', error); | ||
} | ||
|
||
client.onStompError = (frame) => { | ||
console.error('Broker reported error: ' + frame.headers['message']); | ||
console.error('Additional details: ' + frame.body); | ||
}; | ||
|
||
client.publish({ destination: '/app/message', body: 'First Message' }); | ||
}, | ||
}); | ||
|
||
export function connect(){ | ||
client.activate(); | ||
import { Client } from "@stomp/stompjs"; | ||
import websocket from "websocket"; | ||
|
||
Object.assign(global, { WebSocket: websocket.w3cwebsocket }); | ||
|
||
var client; | ||
var role; | ||
var sessionId; | ||
|
||
export function connectWebsocket(userRole, sessionID) { | ||
role = userRole; | ||
sessionId = sessionID; | ||
|
||
client = new Client({ | ||
brokerURL: "ws://localhost:8080/callhub", | ||
onConnect: () => { | ||
client.subscribe(`/topic/message-${role}/${sessionId}`, (message) => { | ||
// TODO: handle received message | ||
console.log(`Received: ${message.body}`); | ||
}); | ||
|
||
client.onWebSocketError = (error) => { | ||
console.error("Error with websocket", error); | ||
}; | ||
|
||
client.onStompError = (frame) => { | ||
console.error("Broker reported error: " + frame.headers["message"]); | ||
console.error("Additional details: " + frame.body); | ||
}; | ||
|
||
console.log("connected to websocket"); | ||
}, | ||
}); | ||
client.activate(); | ||
} | ||
|
||
export function disconnect(){ | ||
client.deactivate() | ||
console.log("websocket disconnected"); | ||
export function disconnectWebsocket() { | ||
client.deactivate(); | ||
console.log("websocket disconnected"); | ||
} | ||
|
||
export function sendMessageWebsocket(message) { | ||
client.publish({ | ||
destination: `/app/message-${role}/${sessionId}`, | ||
body: message, | ||
}); | ||
} |