노출되는 이미지가 불편하시겠지만 양해를 구합니다. 노출, 클릭등에 관한 자료로 활용 중입니다.


Node.JS 웹 채팅 (간단한 예제)


소스 구성 : app.js ( package.json) , index.html ( java script )



1. package.json

{
  "_comment" : "0. setup package",
  "name":"chat",
  "version" : "0.0.1",
  "private": "true",
  "dependencies" : {
    "socket.io" : "1.4.2",
    "express" : "4.13.4"
  }
}


2.app.js

- require

// SOCKET.IO Setup
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http); //initialise after http server

- listen

// http.listen
http.listen(3000, function(err) {
    if ( err ) {
        console.log(err);
    } else {
        console.log('listening on *: 3000')
    }
} );

- get / send index.html

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

});

3. edit index.html

<head>
    <!-- 4. definition form -->
    <style>
        #div-chat-display {
            height:350px;
            width:500px;
            overflow-x: hidden;
            overflow-y: auto;
        }
    </style>
</head> 
<body> 
    <!-- 4. definition form -->
    <h3>Just simple your webChat ( v 0.0.1,  @stanwix , 21-Apr-2016 ) </h3>
    <div id="container-id">
        <form id="form-input-userid">
            your ID :  <input size="15" id="input-userid"/>
            <input type="submit" value="Join"/>
        </form>
    </div>
    <div id="container-message" style="display: none;">
        <div id="div-chat-display"></div>
        <form id="form-send-message">
            <input size="35" id="input-message"/>
            <input type="submit" value="Send"/>
        </form>
    </div>
</body> 

4. include javascript

   <!-- 5. setup js script -->
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>

5. javascript for message handle

아래 소스를 참조


소스

https://github.com/sketchout/NodeJs_SocketIO_PartOne


'Application, App > Node.js' 카테고리의 다른 글

Node.js를 CentOS에 설치하는 방법들  (0) 2017.02.07
(1) 소개 - Node.js  (0) 2016.05.20
블로그 이미지

StartGuide

I want to share the basic to programming of each category and how to solve the error. This basic instruction can be extended further. And I have been worked in southeast Asia more than 3 years. And I want to have the chance to work another country.

,