Skip to content

Commit

Permalink
Merge pull request amberframework#150 from amber-crystal/nick/js_sock…
Browse files Browse the repository at this point in the history
…et_update

added options to javascript Socket
  • Loading branch information
fridgerator authored Jul 28, 2017
2 parents 79451bf + 5225945 commit 26fba1c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion assets/js/amber.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Socket } from './websockets/socket'

export default {
module.exports = {
Socket: Socket
}
1 change: 1 addition & 0 deletions assets/js/amber.min.js

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

26 changes: 26 additions & 0 deletions assets/js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var webpack = require('webpack')

module.exports = {
entry: './amber.js',
output: {
filename: 'amber.min.js',
library: 'Amber'
},
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015']
}
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false }
})
]
};
15 changes: 11 additions & 4 deletions assets/js/websockets/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ export class Socket {
}

connect (params) {
return new Promise((resolve, reject) => {
let location = window.location.hostname
if (window.location.port) location += `:${window.location.port}`
this.ws = new WebSocket(`ws://${location}${this.endpoint}`)
let opts = {
location: window.location.hostname,
port: window.location.port,
protocol: window.location.protocol === 'https:' ? 'wss:' : 'ws:',
}

if (params) Object.assign(opts, params)
if (opts.port) opts.location += `:${opts.port}`

return new Promise((resolve, reject) => {
this.ws = new WebSocket(`${opts.protocol}//${opts.location}${this.endpoint}`)
this.ws.onmessage = (msg) => { this.handleMessage(msg) }
this.ws.onopen = () => resolve()
})
Expand Down

0 comments on commit 26fba1c

Please sign in to comment.