socket.Socket Class
Class for nodeJS communication. You configure nodeJS communication by passing a View/socket object to a View, example: socket:{ url:'http://127.0.0.1:1337' }. You can get a reference to this class by calling getSocket
Constructor
socket.Socket
-
config
Parameters:
-
config
Object
Example:
// Server side code:
var io = require('socket.io').listen(1337, "127.0.0.1");
io.sockets.on('connection', function (socket, request) {
socket.on('sayhello', function (person) {
socket.emit('hello', { message:'Hello ' + person.name, success:true });
});
socket.on('chat', function (request) {
socket.broadcast.emit('getmessage', { message:'Person A says: ' + request.message, success:true });
socket.emit('getmessage', { message:'Person A says: ' + request.message, success:true });
});
});
Client side code:
ludo.chat = {};
ludo.chat.Panel = new Class({
Extends:ludo.View,
type:'chat.Panel',
weight:1,
css:{
'background-color':'#FFF',
'overflow-y':'auto'
},
socket:{
url:'http://127.0.0.1:1337'
},
ludoEvents:function () {
this.getSocket().on('getmessage', this.appendMessage.bind(this));
},
appendMessage:function (msg) {
var html = this.getBody().get('html');
html = html + '>' + msg.message + '<br>';
this.getBody().set('html', html);
}
});
ludo.chat.TextPanel = new Class({
Extends:ludo.View,
layout:'cols',
height:30,
css:{
'margin-top':3
},
children:[
{
type:'form.Text',
weight:1,
name:'text'
},
{
type:'form.Button', value:'Send',
name:'send',
width:80
}
],
socket:{
url:'http://127.0.0.1:1337',
emitEvents:['chat']
},
ludoEvents:function () {
this.parent();
this.child['send'].addEvent('click', this.sendMessage.bind(this))
},
sendMessage:function () {
if (this.child['text'].getValue().length > 0) {
this.fireEvent('chat', { message:this.child['text'].getValue()});
this.child['text'].setValue('');
}
}
});
new ludo.Window({
id:'myWindow',
minWidth:100, minHeight:100,
left:50, top:50,
width:410, height:490,
title:'Chat application',
layout:'rows',
children:[
{
type:'chat.Panel'
},
{
type:'chat.TextPanel'
}
]
});
Methods
emit
-
event
-
query
Emit socket event
Example:
{
q: { query },
m: 'module',
s: 'submodule',
c: 'command/event name'
}
"c" will be set to your passed event name "q" will be set to your passed query object "m" will be set to module name of the view(if any) "s" will be set to sub module name of the view(if any)
Properties
Attributes
emitEvents
Array
Array of view/component events to emit to server. When this event is fired, it will be emitted to the server automatically.
Default: undefined
Example:
new ludo.View({
...
socket:{
url:'http://127.0.0.1:1337',
emitEvents:['chat'] // emit the "chat" event
}
specifies that the "chat" event should be sent to NodeJS on the server.
this.fireEvent('chat', { message:this.child['text'].getValue()});
will cause { message:this.child['text'].getValue()} to be sent to the server where you can pick it up with code like this
socket.on('chat', function (data) {
console.log(data.message);
}
url
String
Socket http url, example: http://localhost:1337 URL can also be defined in ludo.config.setSocketUrl()
Default: undefined