NepenTheS adlı üyeden alıntı:
mesajı görüntüle
22
●5.130
socket.on('sendMessage',function(messageDetail){});diye bir fonksiyonumuz olsun. messageDetail parametlerinizi taşıyan bir dizidir.

<html>
<head>
<link rel='stylesheet' href='http://netdna.bootstrapcdn.com/bootswatch/3.1.1/united/bootstrap.min.css'>
<link rel='stylesheet' href='http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css'>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
<script type="text/javascript">
var socket;
$(document).ready(onDocumentReady);
function onDocumentReady()
{
$('#login_form').on('submit', onLoginFormSubmit);
$('#chat_controls').on('submit', onChatControlsSubmit);
$('#users_list').on('click', 'li', onCreatePrivateSession);
}
function onCreatePrivateSession()
{
var id = $(this).data('id');
var username = $(this).find('a').text();
createTab(id, username)
}
function onChatControlsSubmit(event)
{
sendMessage($('#message_input').val());
event.preventDefault();
}
function sendMessage(message)
{
message = $.trim(message);
if(!message.length)
return;
socket.emit("onMessage", currentTab(), message);
$('#message_input').val('')
console.log('send message', currentTab(), message)
}
function onLoginFormSubmit(event)
{
var username = $.trim($('#username').val());
if(username.length < 1)
alert('Kullanici adi bos.');
else
connect("http://localhost:3000", { username: username })
event.preventDefault();
}
function connect(host, data)
{
data = data || { };
socket = io.connect(host, { query: $.param(data) });
socket.on('connect', onConnect);
socket.on("onMessage", onMessage);
socket.on("onSystemMessage", onSystemMessage);
socket.on('onUserConnect', onUserConnect);
socket.on('onUserDisconnect', onUserDisconnect);
socket.on('disconnect', onDisconnect);
socket.on('error', onError);
}
function onError(reason)
{
alert(reason);
}
function onConnect()
{
$('#login_form').hide();
$('#rooms').show();
}
function onDisconnect()
{
$('rooms_list').empty();
}
function onMessage(clientId, username, message)
{
appendMessage(clientId, username, message);
}
function onSystemMessage(message)
{
appendMessage(currentTab(), '[Sistem]', message);
}
function appendMessage(clientId, username, message)
{
if(!hasTab(clientId).length)
createTab(clientId, username)
$('#rooms').find('#' + clientId).append('<li><strong class="username">' + username + "</strong>: " + message)
}
function createTab(clientId, username)
{
$('#rooms_list').append('<li><a id="_' + clientId + '"" href="#' + clientId +'" data-toggle="tab">' + username + '</a></li>');
$('#rooms').append('<ul class="tab-pane" id="' + clientId + '"></ul>')
$('#rooms_list li a').last().tab('show')
}
function deleteTab(clientId)
{
$('#rooms_list').find('#_' + clientId).remove();
$('#rooms').find('#' + clientId).remove();
}
function hasTab(clientId)
{
return $('#rooms').has('#' + clientId);
}
function currentTab()
{
return $('#rooms_list').find('.active a').attr('id').slice(1);
}
function onUserConnect(id, username)
{
console.log('On user connect.', id, username);
$('#users_list').append(_createUser(id, username))
}
function _createUser(id, username)
{
return '<li data-id="' + id + '"" ><a href="#" class="btn btn-primary">' + username + '</a></li>'
}
function onUserDisconnect(id)
{
console.log('On user disconnect.', id, username);
$('#users_list').find('li[data-id=' + id + ']').remove();
}
</script>
<style>
.container
{
margin-top: 30px;
}
#main
{
height: 550px;
}
#rooms
{
display: none;
}
#rooms .tab-pane
{
height: 85%;
list-style: none;
margin: 0;
padding: 10px;
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
#rooms .tab-pane li
{
padding: 10px;
border-bottom: 1px solid #EFEFEF;
}
#login_form
{
margin-top: 30px;
padding: 10px;
border: 1px solid #CCC;
background: #EFEFEF;
}
</style>
</head>
<body>
<div class='container'>
<div class='col-md-2'>
<ul id='users_list' class="nav nav-pills nav-stacked">
</ul>
</div>
<div id='main' class='col-md-10'>
<ul id='rooms_list' class="nav nav-tabs">
<li class="active"><a id='_lobby' href="#lobby" data-toggle="tab">Lobi</a></li>
</ul>
<div id='rooms' class="tab-content">
<ul class="tab-pane active" id="lobby">
</ul>
</div>
<form id='login_form' class='col-md-6 col-md-offset-3'>
<div class='form-group'>
<input id='username' class='form-control fa fa-heart' type="text" placeholder='Kullanici Adi'/>
</div>
<div class='form-group'>
<input class='form-control' type='submit' value='Baglan'>
</div>
</form>
</div>
<div class="col-md-10 col-md-offset-2">
<form id='chat_controls' class="row">
<div class='col-md-10'>
<input id='message_input' class='form-control' type="text"/>
</div>
<div class='col-md-2'>
<input class='form-control' type='submit' value='Gonder'>
</div>
</form>
</div>
</body>
</html>Coffeescript kodları.class Client
constructor: (@server, @socket, @username) ->
@server.sendAll('onUserConnect', @id(), @username)
@_initUsers()
@_initCallbacks()
_initCallbacks: =>
@socket.on('onMessage', @_onMessage)
@socket.on('disconnect', @_onDisconnect)
_initUsers: =>
@send('onUserConnect', id, client.username) for id, client of @server.clients
_onMessage: (clientId, message) =>
return @server.sendAll('onMessage', clientId, @username, message) if clientId == 'lobby'
if @server.getClient(clientId)
@send('onMessage', clientId, @username, message)
@server.getClient(clientId).send('onMessage', @id(), @username, message)
else
@send('onSystemMessage', 'Bu kullanici cevirim disi.')
_onDisconnect: =>
@server.sendAll('onUserDisconnect', @id())
delete @server.clients[@id()]
id: =>
@socket.id
send: (args...) =>
@socket.emit.apply(@socket, args)
broadcast: (args...) =>
@socket.broadcast.emit.apply(@socket.broadcast, args)
class Server
instance = null
@get: ->
unless instance?
instance = new @
instance._init()
instance
_init: ->
@clients = { }
@io = require("socket.io")
_initCallbacks: =>
@io.set 'authorization', @_authorization
@io.sockets.on "connection", @_onConnect
_authorization: (handshake, callback) =>
username = handshake.query.username
handshake.username = username
for id, client of @clients
return callback('Bu kullanıcı su an bagli.', false) if client.username == username
callback(null, true)
_onConnect: (socket) =>
@clients[socket.id] = new Client(@, socket, socket.handshake.username)
start: =>
@io = @io.listen(3000)
@_initCallbacks()
console.log("Sunucu dinliyor.")
sendAll: (args...) =>
@io.sockets.emit.apply(@io.sockets, args)
broadcast: (args...) =>
@io.sockets.broadcast.apply(@io.sockets, args)
getClient: (id) =>
@clients[id]
Server.get().start()Derlenmiş Javascipt kodları.// Generated by CoffeeScript 1.6.1
(function() {
var Client, Server,
_this = this,
__slice = [].slice;
Client = (function() {
function Client(server, socket, username) {
var _this = this;
this.server = server;
this.socket = socket;
this.username = username;
this.broadcast = function broadcast() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Client.prototype.broadcast.apply(_this, arguments);
};
this.send = function send() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Client.prototype.send.apply(_this, arguments);
};
this.id = function id() {
return Client.prototype.id.apply(_this, arguments);
};
this._onDisconnect = function _onDisconnect() {
return Client.prototype._onDisconnect.apply(_this, arguments);
};
this._onMessage = function _onMessage(clientId, message) {
return Client.prototype._onMessage.apply(_this, arguments);
};
this._initUsers = function _initUsers() {
return Client.prototype._initUsers.apply(_this, arguments);
};
this._initCallbacks = function _initCallbacks() {
return Client.prototype._initCallbacks.apply(_this, arguments);
};
this.server.sendAll('onUserConnect', this.id(), this.username);
this._initUsers();
this._initCallbacks();
}
Client.prototype._initCallbacks = function _initCallbacks() {
this.socket.on('onMessage', this._onMessage);
return this.socket.on('disconnect', this._onDisconnect);
};
Client.prototype._initUsers = function _initUsers() {
var client, id, _ref, _results;
_ref = this.server.clients;
_results = [];
for (id in _ref) {
client = _ref[id];
_results.push(this.send('onUserConnect', id, client.username));
}
return _results;
};
Client.prototype._onMessage = function _onMessage(clientId, message) {
if (clientId === 'lobby') {
return this.server.sendAll('onMessage', clientId, this.username, message);
}
if (this.server.getClient(clientId)) {
this.send('onMessage', clientId, this.username, message);
return this.server.getClient(clientId).send('onMessage', this.id(), this.username, message);
} else {
return this.send('onSystemMessage', 'Bu kullanici cevirim disi.');
}
};
Client.prototype._onDisconnect = function _onDisconnect() {
this.server.sendAll('onUserDisconnect', this.id());
return delete this.server.clients[this.id()];
};
Client.prototype.id = function id() {
return this.socket.id;
};
Client.prototype.send = function send() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.socket.emit.apply(this.socket, args);
};
Client.prototype.broadcast = function broadcast() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.socket.broadcast.emit.apply(this.socket.broadcast, args);
};
return Client;
})();
Server = (function() {
var instance;
function Server() {
var _this = this;
this.getClient = function getClient(id) {
return Server.prototype.getClient.apply(_this, arguments);
};
this.broadcast = function broadcast() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Server.prototype.broadcast.apply(_this, arguments);
};
this.sendAll = function sendAll() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Server.prototype.sendAll.apply(_this, arguments);
};
this.start = function start() {
return Server.prototype.start.apply(_this, arguments);
};
this._onConnect = function _onConnect(socket) {
return Server.prototype._onConnect.apply(_this, arguments);
};
this._authorization = function _authorization(handshake, callback) {
return Server.prototype._authorization.apply(_this, arguments);
};
this._initCallbacks = function _initCallbacks() {
return Server.prototype._initCallbacks.apply(_this, arguments);
};
}
instance = null;
Server.get = function get() {
if (instance == null) {
instance = new this;
instance._init();
}
return instance;
};
Server.prototype._init = function _init() {
this.clients = {};
return this.io = require("socket.io");
};
Server.prototype._initCallbacks = function _initCallbacks() {
this.io.set('authorization', this._authorization);
return this.io.sockets.on("connection", this._onConnect);
};
Server.prototype._authorization = function _authorization(handshake, callback) {
var client, id, username, _ref;
username = handshake.query.username;
handshake.username = username;
_ref = this.clients;
for (id in _ref) {
client = _ref[id];
if (client.username === username) {
return callback('Bu kullanıcı su an bagli.', false);
}
}
return callback(null, true);
};
Server.prototype._onConnect = function _onConnect(socket) {
return this.clients[socket.id] = new Client(this, socket, socket.handshake.username);
};
Server.prototype.start = function start() {
this.io = this.io.listen(3000);
this._initCallbacks();
return console.log("Sunucu dinliyor.");
};
Server.prototype.sendAll = function sendAll() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.io.sockets.emit.apply(this.io.sockets, args);
};
Server.prototype.broadcast = function broadcast() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.io.sockets.broadcast.apply(this.io.sockets, args);
};
Server.prototype.getClient = function getClient(id) {
return this.clients[id];
};
return Server;
})();
Server.get().start();
}).call(this);