Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Created August 23, 2013 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilukasz/6321384 to your computer and use it in GitHub Desktop.
Save hilukasz/6321384 to your computer and use it in GitHub Desktop.
var Campfire = require("../lib/campfire").Campfire;
var express = require("express");
var app = express();
var port = 3700;
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res){
res.render("page");
});
var io = require('socket.io').listen(app.listen(port));
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
console.log("Listening on port " + port);
////////////////
// Campfire API
////////////////
var instance = new Campfire({
ssl : true,
token : "c702f23c9e282ad04c5053d371fd0f6244ee33b8",
account : "visualhero1"
});
instance.join(571821, function(error, room) {
room.listen(function(message) {
console.log("listening to room")
if (message.body == "/wfh") {
console.log("PING received.");
room.speak("working from home", function(error, response) {
console.log("PONG sent at " + response.message.created_at + ".");
});
room.speak("user ID: "+message.userId);
var text = "working from home";
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
} else {
console.log("Received unknown message:");
console.log(message);
}
});
});
window.onload = function() {
var messages = [];
var socket = io.connect('http://localhost:3700');
var field = document.getElementById("field");
var sendButton = document.getElementById("send");
var content = document.getElementById("content");
var name = document.getElementById("name");
socket.on('message', function (data) {
if(data.message) {
messages.push(data);
var html = '';
for(var i=0; i<messages.length; i++) {
html += '<b>' + (messages[i].username ? messages[i].username : 'Server') + ': </b>';
html += messages[i].message + '<br />';
}
content.innerHTML = html;
} else {
console.log("There is a problem:", data);
}
});
}
!!!
html
head
title= "Real time web chat"
script(src='/logic.js')
script(src='/socket.io/socket.io.js')
body
#content(style='width: 500px; height: 300px; margin: 0 0 20px 0; border: solid 1px #999; overflow-y: scroll;')
.controls
| Name:
input#name(style='width:350px;')
br
input#field(style='width:350px;')
input#send(type='button', value='send')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment