2015-02-06 2 views
0

를 전송 및/또는 잡하지 채팅 Socket.IO에 : https://www.youtube.com/watch?v=pNKNYLv2BpQ내가 socket.io 채팅 튜토리얼을 따라 한 메시지

를 튜토리얼 비디오에서는 그러나 나의이 전혀 작동하지 않습니다 잘 작동합니다. 메시지는 이것은 내 index.html을 클라이언트

있는 서버 또는이 내 app.js 노드

var express = require('express'), 
app = express(), 
server = require('http').createServer(app), 
io = require('socket.io').listen(server); 

server.listen(3000); 

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

io.sockets.on('connection', function(socket) { 
    socket.on('send message', function(data) { 
     io.sockets.emit('new message', data); 
    }); 
}); 

와 서버 측에서 실행되는

클라이언트 중 하나에 의해 전송하거나 수신하지 않는 것

<html> 
<head> 
<title>Chat with socket.io and node.js</title> 
<style> 
    #chat 
    { 
     height: 500px; 
    } 
</style> 
</head> 
<body> 
<div id="chat"></div> 
<form id="send-message"> 
    <input size="35" id="message"></input> 
    <input type="submit"></input> 
</form> 

<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script src="/socket.io/socket.io.js"></script> 
<script> 
    jQuery(function($) { 
     var socket = io.connect(); 
     var $messageForm = $('#send-message'); 
     var $messageBox = $('#message'); 
     var $chat $('#chat'); 

     $messageForm.submit(function(e) { 
      e.preventDefault(); 
      socket.emit('send message', $messageBox.val()); 
      $messageBox.val(''); 
     }); 

     socket.on('new message', function(data) { 
      $chat.append(data + "<br/>"); 
     }); 
    }); 
</script> 

가 나는, 어떤 아이디어를 내가 간과하지만 난 순간 난처한 상황에 빠진거야 간단한 오류가 가정 ??? 추가 정보가 필요한 경우도 그렇게 말하십시오 :

+0

이렇게하면됩니다. var io = require ('socket.io') (server); – nalinc

+0

@NLN 해결책을 찾으면 다음과 같이 나타납니다. /home/snapper26/Desktop/chat/app.js:4 \t io = require ('socket.io') (서버); \t^ TypeError : 개체가 함수가 아닙니다. at Object. (/home/snapper26/Desktop/chat/app.js:4:27) 012. 모듈 확장 모듈 (module.js : 476 : 10) at Module.load (module.js : 356 : 32) Function.Module._load (module.js : 312 : 12) Function.Module.runMain (module.js : 497 : 10) at 시작 (node.js : 119 : 16) at node.js : 906 : 3 – Snapper26

답변

0

하나

index.html을

<!doctype html> 
<html> 
    <head> 
    <title>Socket.IO chat</title> 
    <style> 
     * { margin: 0; padding: 0; box-sizing: border-box; } 
     body { font: 13px Helvetica, Arial; } 
     form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } 
     form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } 
     form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } 
     #messages { list-style-type: none; margin: 0; padding: 0; } 
     #messages li { padding: 5px 10px; } 
     #messages li:nth-child(odd) { background: #eee; } 
    </style> 
    </head> 
    <body> 
    <ul id="messages"></ul> 
    <form action=""> 
     <input id="m" autocomplete="off" /><button>Send</button> 
    </form> 
    </body> 
</html> 


<script> 
    var socket = io(); 
    $('form').submit(function(){ 
    socket.emit('chat message', $('#m').val()); 
    $('#m').val(''); 
    return false; 
    }); 
    socket.on('chat message', function(msg){ 
    $('#messages').append($('<li>').text(msg)); 
    }); 
</script> 

하는 index.js

var app = require('express')(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 

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

io.on('connection', function(socket){ 
    console.log('a user connected'); 
socket.on('chat message', function(msg){ 
    console.log('message: ' + msg); 
    }); 
}); 

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

For more information click here 파일 index.html을 생성

관련 문제