Skip to content

Instantly share code, notes, and snippets.

@hopewise
Created May 3, 2016 14:43
Show Gist options
  • Save hopewise/b98227b022f0d0ba8451813044b1fe02 to your computer and use it in GitHub Desktop.
Save hopewise/b98227b022f0d0ba8451813044b1fe02 to your computer and use it in GitHub Desktop.
app.js for phoenix framework chatting example
// Brunch automatically concatenates all files in your
// watched paths. Those paths can be configured at
// config.paths.watched in "brunch-config.js".
//
// However, those files will only be executed if
// explicitly imported. The only exception are files
// in vendor, which are never wrapped in imports and
// therefore are always executed.
// Import dependencies
//
// If you no longer want to use a dependency, remember
// to also remove its path from "config.paths.watched".
import "phoenix_html"
// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".
// import socket from "./socket"
import socket from "./socket"
class App {
static init() {
console.log("Initialized")
var username = $("#username")
var msgBody = $("#message")
let socket = new Socket("/socket")
socket.connect()
socket.onClose( e => console.log("Closed connection") )
var channel = socket.channel("rooms:lobby", {})
channel.join()
.receive( "error", () => console.log("Connection error") )
msgBody.off("keypress")
.on("keypress", e => {
if (e.keyCode == 13) {
console.log('[${username.val()}] ${msgBody.val()}')
msgBody.val("")
}
})
}
}
$( () => App.init() )
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment