1. 程式人生 > >nodejs websocket

nodejs websocket

lis list index hello ebs tails mit https fun

https://blog.csdn.net/qq_30100043/article/details/53005478

<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script type="text/javascript">

   // var socket = io.connect(‘ws://127.0.0.1:3001‘);
    var socket = io.connect("//127.0.0.1:3001");
    socket.on(news, function (data) {
        console.log(data);
        alert(data);
        socket.emit(
my other event, { my: data }); }); </script>
var app = require(‘express‘)();
var http = require(‘http‘).Server(app);
var io = require(‘socket.io‘)(http);
var fs = require(‘fs‘);


app.use(function(req, res, next){
    res.io = io;
    next();
});
//#默認打開文件
app.get(‘/‘, function(req, res){
    res.sendfile(‘./index.html‘);
});


io.on(‘connection‘, function (socket) {
    socket.emit(‘news‘, { hello: ‘world1--lala‘ });
    socket.on(‘my other event‘, function (data) {
        console.log(data);
    });
});

// 服務器偵聽在sockettest.com的3000端口上
http.listen(3001, function(){
    //# 輸出到標準輸出
    console.log(‘:3001‘);
});

nodejs websocket