Last active
December 15, 2020 21:05
-
-
Save theodox/8684724 to your computer and use it in GitHub Desktop.
A simple server that can be run inside photoshop, demonstrating how to respond to socket calls
This file contains 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
// requires photoshop CS5+ | |
// create a new socket | |
conn = new Socket(); | |
var keep_serving = true; | |
// sample functions. In a real application you'd have handler functions that could accept more complex inputs | |
var alrt = alert; // pop a dialog | |
var newLayer = function () { return app.activeDocument.artLayers.add(); }; // make a layer | |
var stop = function () { keep_serving = false; }; // stop the server | |
// 'register' the commands by putting them into a dictionary | |
var known_cmds = { 'alert': alrt, 'stop': stop, 'newLayer': newLayer }; | |
while (keep_serving) { | |
if (conn.listen(8789)) // ... you'd probably want to make this configurable | |
{ | |
// wait forever for a connection | |
var incoming; | |
do incoming = conn.poll(); | |
while (incoming == null); | |
// grab the next non-null communication | |
new_cmd = incoming.read(); | |
try { | |
// split the incoming message into cmd on spaces (shell style) | |
var command_text = new_cmd.split(" ", 1); | |
var args = new_cmd.slice(command_text[0].length + 1, 999).split(" "); | |
var requested = known_cmds[command_text]; | |
if (null != requested) { | |
result = requested(args); | |
incoming.writeln(result + "\nOK\n"); | |
} | |
else { | |
incoming.writeln("unknown command\nFAIL\n"); | |
} | |
} | |
catch (err) { | |
incoming.writeln(err + "FAIL\n"); | |
incoming.close(); | |
delete incoming; | |
} | |
} // end if | |
} // -- end while |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment