아꿈사 스터디 블로그에서 꽃집총각님이 작성하신 글을 보고 직접 구현해 보았습니다.
윈도우용 설치 파일을 다운로드 받아 설치한다.
그리고 cmd 명령창에서 다음 명령을 실행하여 socket.io를 설치한다.
npm install socket.io
새로운 폴더(d:\remoteWeb)를 생성하고 다음과 같이 파일을 작성한다.
server.js
var io = require('socket.io');
var server = require('http').createServer(serverHandler)
, filesystem = require('fs')
, cmd = require('child_process')
, io = io.listen(server);
server.listen(serverPort(80));
function serverPort(portNumber)
{
console.log("Server is running, Port : " + portNumber);
return portNumber;
}
function serverHandler(request, result)
{
filesystem.readFile(__dirname + '/index.html',
function (error, data)
{
if (error)
{
result.writeHead(500);
return result.end('Error loading index.html');
}
result.writeHead(200);
result.end(data);
}
);
}
io.sockets.on('connection', function (socket)
{
socket.on('execute', function (data)
{
var name;
var value;
for (index in data)
{
name = index;
value = data[index];
}
packetHandler[value]();
});
});
var packetHandler =
{
'calc on' : function() { cmd.exec('calc'); },
'calc off' : function() { cmd.exec('taskkill /f /im calc.exe'); },
}
index.html
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.0.4/');
function execute(command)
{
socket.emit('execute', {cmd: command});
}
</script>
</head>
<body>
<button type="button" style="width:300px;height:150px;font-size:40pt;" onclick="execute('calc on')">Calc on</button>
<button type="button" style="width:300px;height:150px;font-size:40pt;" onclick="execute('calc off')">Calc Off</button>
</body>
</html>cmd 명령창에서 다음과 같이 입력한다.
cd /d "d:\remoteWeb"
node server.js
이제 서버가 정상적으로 실행 되었으므로, 웹페이지에서 확인을 하면 된다.
http://192.168.0.4 로 접속한다.
(자신의 환경에 맞는 IP로 index.html을 수정하고 사용하시면 됩니다.)
덧글을 달아 주세요