NodeJS+Express+Socket.io的一个简单例子

初始化一个NodeJS web应用:

打开CMD窗口。

运行cmd:“mkdir myapp”,新建一个文件夹,名为myapp。

运行cmd:“cd myapp”,切换到文件夹myapp。

运行cmd:“npm init”,创建文件package.json。

3.Express入门应用:

在上面的CMD窗口运行cmd:“npm install express --save”, 安装“express” node_module, "--save"表示保存express到package.json。

添加文件app.js到myapp文件夹,假设package.json里面的main是app.js。

//app.js

var app = require(‘express‘)();
var http = require(‘http‘).Server(app);

app.get(‘/‘, function(req, res){
  res.send(‘<h1>Hello world</h1>‘);
});

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

运行cmd:“node app”,启动myapp。

在Chrome里打开“http://localhost:3000/”,显示“Hello world”。

在CMD窗口里输入“Ctrl+C”,可终止myapp的运行。

4.Socket.io入门应用

在上面的CMD窗口运行:“npm install --save socket.io”。

添加index.html到myapp文件夹,内容如下:

<!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>

    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <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>
  </body>
</html>

修改app.js如下:

//app.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);

    io.emit(‘chat message‘, msg);
  });

});

app.set(‘port‘, process.env.PORT || 3000);

var server = http.listen(app.get(‘port‘), function() {
  console.log(‘start at port:‘ + server.address().port);
});

再次运行“node index”,用Chrome打开“http://localhost:3000/”,显示如下“

引用:

http://www.wenjuan.com/s/MrmyYv

http://www.wenjuan.com/s/qUFzyi

http://www.wenjuan.com/s/MrmyYv

http://www.wenjuan.com/s/AVfUBfe

http://www.wenjuan.com/s/ZVfm2iF

http://www.wenjuan.com/s/QBV7Nf

http://www.wenjuan.com/s/2U3Q3u

http://www.wenjuan.com/s/3meE3y

http://www.wenjuan.com/s/M3Qr6fb

http://www.wenjuan.com/s/AZnuya

http://www.wenjuan.com/s/A77ZRz

http://www.wenjuan.com/s/bEVfIb

http://www.wenjuan.com/s/AbAzAjS

http://www.wenjuan.com/s/v6zY3m

http://www.wenjuan.com/s/ENBBZb

http://www.wenjuan.com/s/3mMbQf

http://www.wenjuan.com/s/MvMfMn

http://www.wenjuan.com/s/iAJZJj

http://www.wenjuan.com/s/n67zYj

http://www.wenjuan.com/s/3Qj6Fz

http://www.wenjuan.com/s/viimYv

http://www.wenjuan.com/s/Qrqm6b

http://www.wenjuan.com/s/JFNR32

http://www.wenjuan.com/s/Njemmi

http://www.wenjuan.com/s/aMNnIb

http://www.wenjuan.com/s/jMRrYz

http://www.wenjuan.com/s/VJJFrq

http://www.wenjuan.com/s/QVBjIne

http://www.wenjuan.com/s/yamU3y

http://www.wenjuan.com/s/ey2A7j

http://www.wenjuan.com/s/INFJBf

http://www.wenjuan.com/s/RBZJru

http://www.wenjuan.com/s/7z2YZf

http://www.wenjuan.com/s/UfyMba

http://www.wenjuan.com/s/B3IN3i

http://www.wenjuan.com/s/AbA7Zb

http://www.wenjuan.com/s/QbaiQf

http://www.wenjuan.com/s/z2mmIv

http://www.wenjuan.com/s/EnUbYn

http://www.wenjuan.com/s/eQfYNr

http://www.wenjuan.com/s/A7nAZn

http://www.wenjuan.com/s/yAviqe

http://www.wenjuan.com/s/e2YrIr

http://www.wenjuan.com/s/A7nAZn

http://www.wenjuan.com/s/yAviqe

http://www.wenjuan.com/s/YRb6R3

http://www.wenjuan.com/s/YrquQj

http://www.wenjuan.com/s/UJbIRvP

http://www.wenjuan.com/s/r2mMri

http://www.wenjuan.com/s/aaqUru

http://www.wenjuan.com/s/YnmYN3

http://www.wenjuan.com/s/fqUree

http://www.wenjuan.com/s/v6neyi

http://www.wenjuan.com/s/Vjyiii

http://www.wenjuan.com/s/f6JzEv

http://www.wenjuan.com/s/Rb6Fby

http://www.wenjuan.com/s/JZNvIj

http://www.wenjuan.com/s/vumErq

http://www.wenjuan.com/s/NjQNfmH

http://www.wenjuan.com/s/raYNJr

http://www.wenjuan.com/s/EBVRfq

http://www.wenjuan.com/s/7Z3m6b

http://www.wenjuan.com/s/JB7bEj

http://www.wenjuan.com/s/q6ZRJ3

http://www.wenjuan.com/s/yQRjaq

http://www.wenjuan.com/s/me6fqe

http://www.wenjuan.com/s/Y7nIzu

http://www.wenjuan.com/s/YZfaQn

http://www.wenjuan.com/s/JRfQNz

http://www.wenjuan.com/s/RVjU7r

http://www.wenjuan.com/s/InqU3q

http://www.wenjuan.com/s/673qui

http://www.wenjuan.com/s/FJvQzi

http://www.wenjuan.com/s/7nIVVn

http://www.wenjuan.com/s/VJJJVr

http://www.wenjuan.com/s/V32eQr

http://www.wenjuan.com/s/eYNzm2

http://www.wenjuan.com/s/FfYrQv

http://www.wenjuan.com/s/VnIJNj

http://www.wenjuan.com/s/qUfYNv

http://www.wenjuan.com/s/vA3MVv

http://www.wenjuan.com/s/6jMNrm

http://www.wenjuan.com/s/uqiuae

http://www.wenjuan.com/s/jAfiqy

http://www.wenjuan.com/s/MfeiEn

http://www.wenjuan.com/s/BRb2Mr

http://www.wenjuan.com/s/m6nAju

http://www.wenjuan.com/s/VZvUBv

http://www.wenjuan.com/s/6JniEv

http://www.wenjuan.com/s/VBnmym

http://www.wenjuan.com/s/vy2ae2

http://www.wenjuan.com/s/e2mMrq

http://www.wenjuan.com/s/6B732y

http://www.wenjuan.com/s/UFvEbu

http://www.wenjuan.com/s/rq6vya

http://www.wenjuan.com/s/mqaeY3

http://www.wenjuan.com/s/f2eUfq

http://www.wenjuan.com/s/FBBbme

http://www.wenjuan.com/s/uQVreq3

http://www.wenjuan.com/s/ieIZZb

http://www.wenjuan.com/s/jqIbai

http://www.wenjuan.com/s/NBNvAv

http://www.wenjuan.com/s/qMr6jm

http://www.wenjuan.com/s/n2YrQb

http://www.wenjuan.com/s/yAfYRn

http://www.wenjuan.com/s/nQFVn2

http://www.wenjuan.com/s/UzIV7f

http://www.wenjuan.com/s/JVZ3uq

http://www.wenjuan.com/s/UvaAZv

http://www.wenjuan.com/s/f2eUfq

http://www.wenjuan.com/s/IrQBniM

http://www.wenjuan.com/s/mqaeY3

http://www.wenjuan.com/s/rq6vya

http://www.wenjuan.com/s/UFvEbu

http://www.wenjuan.com/s/r6VjM3

http://www.wenjuan.com/s/UBJ7Fz

http://www.wenjuan.com/s/zi2i22

http://www.wenjuan.com/s/mIRfAj

http://www.wenjuan.com/s/67RbU3

http://www.wenjuan.com/s/Ina2me

http://www.wenjuan.com/s/z6byMj

http://www.wenjuan.com/s/I3IZF3

http://www.wenjuan.com/s/BjqeiyO

http://www.wenjuan.com/s/RNfUNnE

http://www.wenjuan.com/s/RNfUNnE

http://www.wenjuan.com/s/7Rj6ne

http://www.wenjuan.com/s/FnA7fu

http://www.wenjuan.com/s/NJjEVn

http://www.wenjuan.com/s/6nAJzq

http://www.wenjuan.com/s/jIjqY3

http://www.wenjuan.com/s/jieaye

http://www.wenjuan.com/s/RvmIju

http://www.wenjuan.com/s/JJzaUz

http://www.wenjuan.com/s/fQZ3ya

http://www.wenjuan.com/s/NnqMjm

http://www.wenjuan.com/s/auARzy

http://www.wenjuan.com/s/vaEFJ3

http://www.wenjuan.com/s/vaEFJ3

http://www.wenjuan.com/s/AnuMVfR

http://www.wenjuan.com/s/7vaya2S

http://www.wenjuan.com/s/q6Z3Qb

http://www.wenjuan.com/s/AnMnMz

http://www.wenjuan.com/s/A3IrQr

hthttp://www.wenjuan.com/s/AV73qi

http://www.wenjuan.com/s/QRjmUn

http://www.wenjuan.com/s/vUR32e

http://www.wenjuan.com/s/Y7Rbii

http://www.wenjuan.com/s/2umyqm

http://www.wenjuan.com/s/rIBZvq

http://www.wenjuan.com/s/eemA3i

http://www.wenjuan.com/s/EVBV3i

http://www.wenjuan.com/s/3EbI7zL

http://www.wenjuan.com/s/mYriAf

http://www.wenjuan.com/s/eYNviqb

http://www.wenjuan.com/s/fQ3Qzu

http://www.wenjuan.com/s/eUBN3e

http://c.tieba.baidu.com/p/3373993333

http://c.tieba.baidu.com/p/3372926228

http://www.wenjuan.com/s/2YvYf2

http://www.wenjuan.com/s/r6zMzi

http://www.wenjuan.com/s/7fu2AfU

http://www.wenjuan.com/s/rUneiy

http://www.wenjuan.com/s/7Fre6z

http://www.wenjuan.com/s/fyaIva

http://www.wenjuan.com/s/RZFnEz

http://www.wenjuan.com/s/ia6rYz

http://www.wenjuan.com/s/jimURr

http://www.wenjuan.com/s/YbARfe

http://www.wenjuan.com/s/Iz67jq

http://www.wenjuan.com/s/F7n632

http://www.wenjuan.com/s/NbIZVj

http://www.wenjuan.com/s/6BjEbu

http://www.wenjuan.com/s/vuqUjy

http://www.wenjuan.com/s/YbaiIr

http://www.wenjuan.com/s/3aquEb

http://www.wenjuan.com/s/Nf2UNb

http://www.wenjuan.com/s/YNFNJz

http://www.wenjuan.com/s/n6j6R3

http://www.wenjuan.com/s/aUz2Ib

http://www.wenjuan.com/s/quqm6b

http://www.wenjuan.com/s/vMnqay

http://www.wenjuan.com/s/YVFVvm

http://www.wenjuan.com/s/buEzqq

时间: 2024-10-18 01:12:08

NodeJS+Express+Socket.io的一个简单例子的相关文章

node+express+socket.io制作一个聊天室功能

首先是下载包: npm install express npm install socket.io 建立文件: 服务器端代码:server.js 1 var http=require("http"); 2 var express=require("express"); 3 var sio=require("socket.io"); 4 5 var app=express(); 6 app.use(express.static(__dirname+

TWaver html5 + NodeJS + express + websocket.io + redis 快速搭建项目(一)

最近看到TWaver 的html5版本即将发布了,于是今天用TWaver HTML5 + NodeJS + express + websocket.io + redis搭建了一个简单原型.先发出几张效果图给大家尝尝鲜.界面添加.修改以及删除网元后,会自动增量存库,并广播更新. 看看这个广播实现的效果,可以在手机上接受任何操作: 最后再来个告警广播的效果图 在后续的文章中会给大家详细的讲解具体技术的实现.

vue + socket.io实现一个简易聊天室

vue + vuex + elementUi + socket.io实现一个简易的在线聊天室,提高自己在对vue系列在项目中应用的深度.因为学会一个库或者框架容易,但要结合项目使用一个库或框架就不是那么容易了.功能虽然不多,但还是有收获.设计和实现思路较为拙劣,恳请各位大大指正. 可以达到的需求 能查看在线用户列表 能发送和接受消息 使用到的框架和库 socket.io做为实时通讯基础 vuex/vue:客户端Ui层使用 Element-ui:客户端Ui组件 类文件关系图 服务端: 客户端: 服

使用socket编程实现一个简单的文件服务器

使用socket编程实现一个简单的文件服务器.客户端程序实现put功能(将一个文件从本地传到文件服务器)和get功能(从文件服务器取一远程文件存为本地文件).客户端和文件服务器不在同一台机器上. put [-h hostname] [-p portname] local_filenameremote_filename get [-h hostname] [-p portname] remote_filenamelocal_filename 程序如下: 客户端Client.java package

从一个简单例子来理解js引用类型指针的工作方式

? 1 2 3 4 5 6 7 <script> var a = {n:1};  var b = a;   a.x = a = {n:2};  console.log(a.x);// --> undefined  console.log(b.x);// --> [object Object]  </script> 上面的例子看似简单,但结果并不好了解,很容易把人们给想绕了--"a.x不是指向对象a了么?为啥log(a.x)是undefined?".&

C语言多线程的一个简单例子

多线程的一个简单例子: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> void * print_a(void *); void * print_b(void *); int main(){ pthread_t t0; pthread_t t1; // 创建线程A if(pthread_creat

生产者与消费者的一个简单例子

生产者 #include<fstream> #include<iostream> #include<Windows.h> using namespace std; int main(void) { ofstream out; const char ch = '*'; long long k = 0; DWORD64 time = GetTickCount64(); while (true) { if (GetTickCount64() - time > 5000)

Nodejs做web服务器的一个简单逻辑和实现

本文写了自己对"Nodejs做web服务器的一个简单逻辑和实现",如果不太合理,或者了解这部分的大神,欢迎拍死.哈哈,新手. 昨天开始学习Nodejs,w3cschool看了一遍.感觉其上的内容,一上来就开始介绍Nodejs的模块.看来后来,连如何"从零创建一个nodejs版的web服务器"都没能明白.可能是自己太过肤浅,或者,陷到了WAMP的思维里,不能自拔吧.后来在nodejs中文社区发了个帖子.感觉自己算是明白了点儿.写出来,希望和大家一起学习,同时也希望得到

用socket.io实现websocket的一个简单例子

http://biyeah.iteye.com/blog/1295196 socket.io的介绍 http://www.cnblogs.com/mazg/p/5467960.html websocket在线测试 http://www.blue-zero.com/WebSocket/