forked from botfront/rasa-webchat
-
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.
- Loading branch information
Showing
10 changed files
with
180 additions
and
25 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
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
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,24 @@ | ||
import io from 'socket.io-client'; | ||
|
||
export default function (socketUrl, customData, path) { | ||
const options = path ? { path } : {}; | ||
const socket = io(socketUrl, options); | ||
socket.on('connect', () => { | ||
console.log(`connect:${socket.id}`); | ||
socket.customData = customData; | ||
}); | ||
|
||
socket.on('connect_error', (error) => { | ||
console.log(error); | ||
}); | ||
|
||
socket.on('error', (error) => { | ||
console.log(error); | ||
}); | ||
|
||
socket.on('disconnect', (reason) => { | ||
console.log(reason); | ||
}); | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import SockJS from 'sockjs-client'; | ||
import { Stomp } from '@stomp/stompjs'; | ||
import { EventEmitter } from 'events'; | ||
|
||
/* | ||
This implementation mimics the SocketIO implementation. | ||
*/ | ||
export default function (socketUrl, customData, _path, options) { | ||
const socket = SockJS(socketUrl); | ||
const stomp = Stomp.over(socket); | ||
|
||
const MESSAGES_CHANNEL = options.messagesChannel || '/app/sendMessage'; | ||
const REPLY_TOPIC = options.replyTopic || '/user/queue/reply'; | ||
const SUBSCRIPTION_CHANNEL = options.subscriptionChannel || '/app/addUser'; | ||
|
||
const socketProxy = new EventEmitter(); | ||
|
||
const send = (message) => { | ||
stomp.send(MESSAGES_CHANNEL, {}, JSON.stringify(message)); | ||
}; | ||
|
||
const extractSessionId = () => { | ||
// eslint-disable-next-line no-underscore-dangle | ||
const urlarray = socket._transport.url.split('/'); | ||
const index = urlarray.length - 2; | ||
return urlarray[index]; | ||
}; | ||
|
||
socketProxy.on('user_uttered', (data) => { | ||
send({ | ||
type: 'CHAT', | ||
content: JSON.stringify(data), | ||
sender: socketProxy.id | ||
}); | ||
}); | ||
|
||
socketProxy.on('session_request', () => { | ||
send({ | ||
type: 'SESSION_REQUEST', | ||
content: JSON.stringify(options.authData || null), | ||
sender: 'client' | ||
}); | ||
}); | ||
|
||
socketProxy.onconnect = () => { | ||
socketProxy.id = extractSessionId(socket); | ||
socketProxy.customData = customData; | ||
stomp.subscribe(REPLY_TOPIC, socketProxy.onIncomingMessage); | ||
stomp.send( | ||
SUBSCRIPTION_CHANNEL, | ||
{}, | ||
JSON.stringify({ type: 'JOIN', sender: socketProxy.id }) | ||
); | ||
}; | ||
|
||
socketProxy.onerror = (error) => { | ||
// eslint-disable-next-line no-console | ||
console.log(error); | ||
}; | ||
|
||
socketProxy.onIncomingMessage = (payload) => { | ||
const message = JSON.parse(payload.body); | ||
|
||
if (message.type === 'JOIN') { | ||
socketProxy.emit('connect'); | ||
} else if (message.type === 'LEAVE') { | ||
socket.close(); | ||
socketProxy.emit('disconnect', 'server left'); | ||
} else if (message.type === 'SESSION_CONFIRM') { | ||
socketProxy.emit('session_confirm', socketProxy.id); | ||
} else if (message.type === 'CHAT') { | ||
const agentMessage = JSON.parse(message.content); | ||
delete agentMessage.recipient_id; | ||
socketProxy.emit('bot_uttered', agentMessage); | ||
} else if (message.type === 'SESSION_CONFIRM') { | ||
socketProxy.emit('session_confirm', message.content); | ||
} | ||
}; | ||
|
||
socketProxy.close = () => { | ||
socket.close(); | ||
}; | ||
|
||
stomp.connect({}, socketProxy.onconnect, socketProxy.onerror); | ||
|
||
socket.onclose = () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Closed sockjs connection'); | ||
socketProxy.emit('disconnect'); | ||
}; | ||
|
||
return socketProxy; | ||
} |
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,24 +1,36 @@ | ||
import io from 'socket.io-client'; | ||
// import io from 'socket.io-client'; | ||
import socketio from './socket-socketio'; | ||
import sockjs from './socket-sockjs'; | ||
|
||
export default function (socketUrl, customData, path) { | ||
const options = path ? { path } : {}; | ||
const socket = io(socketUrl, options); | ||
socket.on('connect', () => { | ||
console.log(`connect:${socket.id}`); | ||
socket.customData = customData; | ||
}); | ||
const PROTOCOLS = { socketio, sockjs }; | ||
// export default function (socketUrl, customData, path) { | ||
// const options = path ? { path } : {}; | ||
// const socket = io(socketUrl, options); | ||
// socket.on('connect', () => { | ||
// console.log(`connect:${socket.id}`); | ||
// socket.customData = customData; | ||
// }); | ||
export default function (socketUrl, customData, path, protocol, protocolOptions) { | ||
protocol = protocol || 'socketio'; | ||
const socketProtocol = PROTOCOLS[protocol]; | ||
|
||
socket.on('connect_error', (error) => { | ||
console.log(error); | ||
}); | ||
if (socketProtocol !== undefined) { | ||
return socketProtocol(socketUrl, customData, path, protocolOptions); | ||
} | ||
throw new Error(`Undefined socket protocol ${protocol}`); | ||
} | ||
|
||
socket.on('error', (error) => { | ||
console.log(error); | ||
}); | ||
// socket.on('connect_error', (error) => { | ||
// console.log(error); | ||
// }); | ||
|
||
socket.on('disconnect', (reason) => { | ||
console.log(reason); | ||
}); | ||
// socket.on('error', (error) => { | ||
// console.log(error); | ||
// }); | ||
|
||
return socket; | ||
}; | ||
// socket.on('disconnect', (reason) => { | ||
// console.log(reason); | ||
// }); | ||
|
||
// return socket; | ||
// }; |