duanbi5906 2015-01-24 10:54
浏览 103
已采纳

SocketIO Nodejs和PHP,如何使其工作?

I have this simple chat app, that is supposed to show received messages in real time without refreshing.

I am running xampp on port 80.

This I have included on index.php

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
    <script src="js/bootstrap.js"></script>
    <script src="http://localhost:80/nodePHP-master\js
ode_modules\socket.io
ode_modules\socket.io-client/socket.io.js"></script>
    <script src="js/nodeClient.js"></script>

nodeClient.js is

    var socket = io.connect( 'http://localhost:80' );

    $( "#messageForm" ).submit( function() {
    var nameVal = $( "#nameInput" ).val();
    var msg = $( "#messageInput" ).val();

    socket.emit( 'message', { name: nameVal, message: msg } );

    // Ajax call for saving datas
    $.ajax({
    url: "./ajax/insertNewMessage.php",
    type: "POST",
    data: { name: nameVal, message: msg },
    success: function(data) {

    }
    });

    return false;
     });

    socket.on( 'message', function( data ) {
    var actualContent = $( "#messages" ).html();
    var newMsgContent = '<li> <strong>' + data.name + '</strong> : ' +        data.message + '</li>';
   var content = newMsgContent + actualContent;

   $( "#messages" ).html( content );
   });

And nodeServer.js is

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

    var app = express();
    var server = http.createServer( app );

    var io = socket.listen( server );

    io.sockets.on( 'connection', function( client ) {
    console.log( "New client !" );

    client.on( 'message', function( data ) {
    console.log( 'Message received ' + data.name + ":" + data.message );

    //client.broadcast.emit( 'message', { name: data.name, message:  data.message } );
    io.sockets.emit( 'message', { name: data.name, message: data.message }   );
    });
    });

    server.listen( 80 ); 

But I keep getting this error message in console :

enter image description here

What am I doing wrong ?

  • 写回答

1条回答 默认 最新

  • dongmei6426 2015-01-24 20:43
    关注

    I've solved it.

    Did not know that I had to start nodeServer.js

    Accessed its location via cmd, and endered node nodeServer.js

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了