Created
September 20, 2012 14:28
-
-
Save barneywilliams/3756269 to your computer and use it in GitHub Desktop.
JSONSocket - Ruby server class snippet
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
def initialize | |
@server = TCPServer.new 19024 | |
end | |
def start | |
@server_thread = Thread.new do | |
@client_thread = Thread.start(@server.accept) do |client| | |
puts "server: Accepted client connection!" | |
send_request(client, 'greeting', :message => 'Hello client!') | |
receive_response(client) | |
send_request(client, 'shutdown') | |
receive_response(client) | |
client.close | |
end | |
end | |
end | |
... | |
def send_request(client, command, args={}) | |
data = {'request' => {'command' => command}} | |
data['request'].merge!(args) | |
request = JSON.generate(data) | |
client.print request | |
if args[:message] | |
puts "server: >> #{args[:message]}" | |
end | |
end | |
def receive_response(client) | |
response = client.recv(1024) | |
data = JSON.parse(response) | |
if data['response'] && data['response']['message'] | |
puts "server: << #{data['response']['message']}" | |
end | |
return data | |
end |
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
QString Client::ProcessCommand(QScriptValue req) | |
{ | |
QScriptValue cmd = req.property("command"); | |
QString response; | |
// Basic sanity check | |
if (!cmd.isValid()) | |
{ | |
response = CommandErrorResponse("invalid", "No command specified"); | |
} | |
// Process greeting | |
else if (cmd.toString() == "greeting") | |
{ | |
QScriptValue message = req.property("message"); | |
if (!message.isValid()) | |
{ | |
response = CommandErrorResponse("greeting", "No message specified"); | |
} | |
else | |
{ | |
response = CommandResponse("greeting", "Hello server. Hope things are well!"); | |
} | |
} | |
... | |
} |
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
tcpSocket = new QTcpSocket(this); | |
... | |
Q_ASSERT(connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(ProcessRequest()))); |
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
void Client::ProcessRequest(void) | |
{ | |
QByteArray request = tcpSocket->readAll(); | |
QScriptEngine engine; | |
// Validate that the request was parsed and non-empty | |
QScriptValue data = engine.evaluate("(" + QString(request) + ")"); | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment