지난 3주동안 업무 중에 웹소켓 구현에 대해 공부하고, 해당 내용을 구현할 방법을 구상하였다.

 

■ 서버를 통한 외부 접속 방식

 

  - 데이터가 인터넷을 통해 서버로 송출,
  - 서버에서 (외부 접속을 통해) 데이터를 수신하고, 로직을 처리.
  - 데이터베이스 저장(할 필요가 없는데도).
  - 서버에서 웹소켓을 통해 데이터 송출
  - 키오스크에서 데이터 수신

이렇게 진행하려고 하였다.

 

■ 에러 사항

1. 웹소켓에 대한 지식이 전무.

2. 실제 예제 연습 중에 윈도우에서는 처리가 되었지만, 리눅스 서버에서 예제를 똑같이 구현해도 같은 결과가 발생하지 않음

  → 리눅스(Ubuntu)에서 Apache 웹 서버를 띄운 상태에서의 추가적인 제약사항. (해결완료)
  → 외부접속을 통해 처리해야하는데 내부접속을 할 수 밖에 없었던 점. (해결못함) 

 

■ 방향의 전환

- 아마 소장님께서 지나가듯이 말씀하셨었다. 
'만약 구현이 제대로 안되는 것 같으면 그냥 윈도우에서 돌리면 어때?  어차피 윈도우 통해서 데이터 받아들이니까.'
이 말씀을 그때는 정신이 팔려서 잘 안들렸지만, 혼자 끙끙대면서 구현해보고, 구상하다보니 마침내 도달한 결론이 되었다...
머쓱.

그래서 기존에는 그림과 같이 카메라→NUC→서버→NUC→키오스크 였는데, 
카메라→NUC→키오스크로 변경하려고 한다. (실제로는 아직 안해봄)
구현 가능성이 넘쳐보이니 일단 시도하려고 한다.

 

 

1단계가 끝나면 그림2와 같이 2단계도 진행하고, 마지막 3단계도 준비되어 있다. 

#WEBSOCKET #SOCKET.IO 

반응형

 

nodejs로 서버를 구성하고, socket.io 를 통해서 웹소켓을 구성하려고하는데 해당 에러가 발생하였다.

 

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

이유인즉슨, result.sendFile( ) 에서 [절대 경로 설정 or Root 경로 설정] 을 해주지 않았기 때문이다.

(라고 stackoverflow에서 말함)

 

그래서 해결방법은 간단하다.

__dirname 을 추가해주면 된다. -> 요청한 경로를 따라옴

app.get('/', function(req, res){
        res.sendFile(__dirname + '/index.html');
        });

 

일단 그래서 해당 에러는 해결이 되었다.

 

 

 

 

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

[add] So my next problem is that when i try adding a new dependence (npm install --save socket.io). The JSON file is also valid. I get this error: Failed to parse json npm ERR! Unexpected string n...

stackoverflow.com

 

 

 

반응형

 

사용환경 :  Ubunut 20.0.4 LTS, Apache2.x, Node.js, Javascript

 

Apache설정 변경

# /etc/apache2/sites-available/000-default.conf  내용 추가

<VirtualHost  *:80>
ServerName example.com

           ProxyRequests Off
           ProxyPreserveHost On
           ProxyVia Full
           <Proxy *>
              Require all granted
           </Proxy>

           <Location /nodejs>
              ProxyPass http://localhost:3000
              ProxyPassReverse http://localhost:3000
           </Location>

            <Directory "/var/www/example.com/html">
                    AllowOverride All
            </Directory>
</VirtualHost>

3000포트로 허용함을 설정

$ sudo service apache2 restart

아파치 서버 재구동 

 

# /var/www/html/nodejs/hello.js

var http = require('http');
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World! Node.js is working correctly.\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
console.log('It is running now...');

마찬가지로 3000포트 연결 설정

 

hello.js 파일 실행

$ node /var/www/html/nodejs/hello.js 

console.log 했던 내용 출력

 

 

http://example.com:8080/node.js/
접속

접속완료

 

 

일단 서버 내에서 8080포트로 접속하여 3000포트의 Websocket communication 로그 출력이 완료되었다.

 

 

 

 

Apache + Node.js + socket.io

코드 이그나이터에서 웹소켓을 돌리는데 드디어 성공! 꽤나 뿌듯하다. 스택 오버플로우와 국내 블로그의 도움을 많이 받았다. 순서대로 오늘 한 일에 대해서 나열해보자면 1. 공유기 포트포워딩

blog.rgbplace.com

 

Set Up a Node.js App for a Website With Apache on Ubuntu 16.04

This tutorial will explain how to set up a Cloud Server running Ubuntu 16.04 so that Node.js scripts run as a service, and configure the Apache server to make the script accessible from the web.

www.ionos.com

 

Behind a reverse proxy | Socket.IO

You will find below the configuration needed for deploying a Socket.IO server behind a reverse-proxy solution, such as:

socket.io

 

반응형

+ Recent posts