Skip to content

Commit

Permalink
Merge pull request #93 from amio-io/92-handle-case-where-external-id-…
Browse files Browse the repository at this point in the history
…not-used-anymore

92-handle-case-where-external-id-not-used-anymore
  • Loading branch information
karelrochelt authored Jun 6, 2024
2 parents 0d2a99a + d251fc0 commit 81deaed
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 24 deletions.
39 changes: 29 additions & 10 deletions lib/amio-chat-sdk-web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amio-chat-sdk-web.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/amio-chat-sdk-web.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/amio-chat-sdk-web.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amio-chat-sdk-web",
"version": "1.6.2",
"version": "1.6.3",
"description": "Amio Chat JavaScript SDK for custom webchat implementation. https://amio.io",
"main": "lib/amio-chat-sdk-web.js",
"module": "src/amio-chat-client.js",
Expand Down
15 changes: 10 additions & 5 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Connection {
const storageType = this.config.storageType || 'local'
this.sessionManager = new SessionManager(storageType)

const sessionId = this.sessionManager.getId()
const sessionId = this.sessionManager.getSessionId()
if(!sessionId) {
// if there is no existing session, we don't want to connect to the server immediately
resolve()
Expand Down Expand Up @@ -105,18 +105,23 @@ class Connection {
}

const serverUrl = this.config._amioChatServerUrl || AMIO_CHAT_SERVER_URL
const sessionId = this.sessionManager.getId()
const sessionId = this.sessionManager.getSessionId()
if(this.config.externalContactId) {
opts.query.external_contact_id = this.config.externalContactId
this.sessionManager.setExternalId(this.config.externalContactId)
} else if(sessionId) {
opts.query.session_id = sessionId
if(this.sessionManager.getExternalId()) {
this.sessionManager.clear()
} else {
opts.query.session_id = sessionId
}
}

this.disconnect()
this.socket = io(serverUrl, opts)

this.socket.on(SOCKET_CONNECTION_ACCEPTED, data => {
this.sessionManager.setId(data.session_id)
this.sessionManager.setSessionId(data.session_id)

this.online = true
this.connectionStateChangedHandler(this.online)
Expand All @@ -139,7 +144,7 @@ class Connection {

this.socket.on('reconnect_attempt', () => {
// if we didn't set the sessionId here, we could end up with a new one after reconnect
const sessionId = this.sessionManager.getId()
const sessionId = this.sessionManager.getSessionId()
if(sessionId) {
this.socket.io.opts.query.session_id = sessionId
}
Expand Down
18 changes: 14 additions & 4 deletions src/connection/session-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
STORAGE_SESSION_NAME
STORAGE_SESSION_NAME,
STORAGE_EXTERNAL_ID
} from '../constants'

class SessionManager {
Expand All @@ -21,16 +22,25 @@ class SessionManager {
}
}

getId() {
getSessionId() {
return this._getStorage().getItem(STORAGE_SESSION_NAME)
}

setId(value) {
getExternalId() {
return this._getStorage().getItem(STORAGE_EXTERNAL_ID)
}

setSessionId(value) {
return this._getStorage().setItem(STORAGE_SESSION_NAME, value)
}

setExternalId(value) {
return this._getStorage().setItem(STORAGE_EXTERNAL_ID, value)
}

clear() {
return this._getStorage().removeItem(STORAGE_SESSION_NAME)
this._getStorage().removeItem(STORAGE_SESSION_NAME)
this._getStorage().removeItem(STORAGE_EXTERNAL_ID)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
AMIO_CHAT_SERVER_URL: 'https://chat.amio.io',

STORAGE_SESSION_NAME: 'amio_chat_session',
STORAGE_EXTERNAL_ID: 'amio_chat_external_id',

SOCKET_IO_DISCONNECT: 'disconnect',
SOCKET_IO_ERROR: 'error',
Expand Down

0 comments on commit 81deaed

Please sign in to comment.