Last active
June 25, 2016 13:59
-
-
Save apb2006/4707605 to your computer and use it in GitHub Desktop.
Node.js + socket.io + BaseX = Xquery chatbot
This file contains hidden or 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
// Andy bunce jan 2013 based on http://book.mixu.net/ch13.html | |
var fs = require('fs'), | |
http = require('http'), | |
sio = require('socket.io'); | |
var port=8001; | |
var server = http.createServer(function(req, res) { | |
res.writeHead(200, { 'Content-type': 'text/html'}); | |
res.end(fs.readFileSync('./index.html')); | |
}); | |
server.listen(port, function() { | |
console.log('Server listening at http://localhost:',port); | |
}); | |
// Attach the socket.io server | |
io = sio.listen(server); | |
// store messages | |
var messages = []; | |
var userId=0; | |
// Define a message handler | |
io.sockets.on('connection', function (socket) { | |
socket.username="user"+userId++; | |
socket.on('message', function (msg) { | |
var lmsg=socket.username+ ": "+msg; | |
messages.push(lmsg); | |
socket.broadcast.emit('message', lmsg); | |
//---- if starts "xquery" execute on basex server ----------- | |
if(msg.indexOf("xquery ") == 0){ | |
bx.execute(msg,function(err,reply){ | |
io.sockets.emit('message', "basex: " +reply.result); | |
}) | |
} | |
}); | |
// send messages to new clients | |
messages.forEach(function(msg) { | |
socket.send(msg); | |
}) | |
}); | |
var basex=require('basex'); | |
var bx = new basex.Session("localhost", 1984, "admin", "admin"); | |
bx.execute("create event chat", afterCreate); | |
// watch for it | |
function afterCreate(err, reply) { | |
console.log("running afterCreate..."); | |
if (err) | |
console.log("Error: " + err); | |
bx.watch("chat", watchCallback); | |
}; | |
// echo events as chat | |
function watchCallback(name,msg){ | |
io.sockets.emit('message', "basex: " +msg); | |
}; |
This file contains hidden or 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
<html> | |
<head> | |
<title>Xquery chat</title> | |
<style type="text/css"> | |
#messages li { padding: 2px 0px; border-bottom: 1px solid #ccc; } | |
</style> | |
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$(function(){ | |
var socket = io.connect(); | |
socket.on('connect', function () { | |
socket.on('message', function(message) { | |
$('#messages').append($('<li></li>').text(message)); | |
}); | |
socket.on('disconnect', function() { | |
$('#messages').append('<li>Disconnected</li>'); | |
}); | |
}); | |
var el = $('#chatmsg'); | |
$('#chatmsg').keypress(function(e) { | |
if(e.which == 13) { | |
e.preventDefault(); | |
socket.send(el.val()); | |
$('#messages').append($('<li></li>').text(el.val())); | |
el.val(''); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<ol id="messages"></ol> | |
<hr> | |
<input type="text" id="chatmsg"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment