消息未发送发出socket.io + codeigniter

时间:2017-12-06 05:08:03

标签: php codeigniter sockets socket.io

我是socket.io中的新用户。我尝试与php框架codeigniter + socket.io进行实时聊天,我尝试在socket.io上执行以下指令,但我遇到了与数据传输相关的限制,数据未发送到客户端,浏览器再次重新加载......

这是我的代码

server:server.js

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

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

var chat = io.of('/chat');
io.on('connection',function(socket){
    socket.on('newMessage', function (msg) {
        io.emit('newMessage', msg);
        console.log('Chat baru: ' + msg);
    });

    socket.on('disconnect', function (msg) {
        console.log('user disconnect');
    });
});

客户端:chat.php

<div class="row-fluid">
    <!-- block -->
    <div class="block">
      <div class="navbar navbar-inner block-header">
          <div class="muted pull-left">Pesan Masuk</div>

      </div>
      <div class="block-content collapse in">
        <div class="span12">
             <div class="msg_box" id="msg_box">
                <div class="frame_msg">
                    <ul class="ul_list">
                    <!-- <h5 name="msg_name">Indra :</h5><p>coba</p>
                 -->
                    </ul>

                 </div>
             </div>

                    <form action="">
                        <div class="block_send">
                            <textarea type="text" id="message" name="message" class="message"></textarea>
                            <button class="btn btn-primary btn-sm btn_submit message_submit" id="message_submit" onclick="test()"></button>  
                        </div>
                    </form>

        </div>
    </div>
    <!-- end /block -->
  </div>
</div>

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="<?php echo base_url();?>/assets/app/site_url.js" ></script>
<script type="text/javascript">
$(function () {
    var socket = io.connect('http://localhost:3000');
var chat = socket.of('/chat');

$('form').submit(function(){
    chat.emit('newMessage',$('#message').val());
    $('#message').val('');
    return false;
});

chat.on('newMessage',function(msg){ 
    $('.ul_list').append('<h5 name="msg_name">Indra :</h5><p>'+msg+'</p>');
}); 
});

</script>
谁能帮帮我? 非常感谢你。

0 个答案:

没有答案