socket消息发送

expressClient.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="./js/jquery-1.3.1.min.js"></script>
<script src="./js/socket.io.js"></script>
<!-- 引入 Bootstrap -->
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
var iosocket=null;
$(function(){
iosocket = io.connect('http://localhost:9000/');
alert(iosocket);
iosocket.on('connect', function () {
iosocket.on('message', function(message) {
//alert('服务器传来:'+message);
myform.content.value += message;
myform.content.value += '<br/>';
});
iosocket.on('disconnect', function() {
alert('和服务器失去连接');
});
})
});
function say(){
var msg = myform.saytext.value;
iosocket.send(msg);
}
</script>
</head>
<body>
<form name='myform' style="margin-left:300px; margin-top:100px">
<!-- 导航栏 -->
<nav class="navbar navbar-default" role="navigation" style="width:730px">
<div class="container-fluid" >
<div class="navbar-header" >
<a class="navbar-brand" href="#" style="margin-left:300px">消息页面</a>
</div>
</div>
</nav>
<!-- 输入栏 -->
<textarea name='content' cols="100" rows="10" style="background-color:#dddddd;" disabled="disabled"></textarea><br/>
<!-- <input type='text' name='saytext' style="margin-top:40px"/> -->
<div style="margin-top:40px;margin-left:100px">
<div class="input-group">
<div style="width:400px">
<input type="text" name='saytext' class="form-control" placeholder="请输入消息"/>
</div>
<div style="margin-left:400px">
<button type="button" class="btn btn-success" value='say' onclick='say()'>发送</button>
</div>
</div>
</div>
<!-- <input type='button' value='say' onclick='say()'/> -->
</form>
</body>
</html>

expressSocketSever.js

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

app.get('/', function(req, res){
fs.readFile('./expressClient.html', function(err, data) {
if(err) {
recall('文件不存在');
}else{
res.send(data.toString());
}
});
});
app.get('/js/jquery-1.3.1.min.js', function(req, res){
fs.readFile('./js/jquery-1.3.1.min.js', function(err, data) {
if(err) {
recall('文件不存在');
}else{
res.send(data.toString());
}
});
});
app.get('/js/socket.io.js', function(req, res){
fs.readFile('./js/socket.io.js', function(err, data) {
if(err) {
recall('文件不存在');
}else{
res.send(data.toString());
}
});
});

//----------Socket服务------------------------------
var ii=0;
var channel={};
io.on('connection', function(socket){
console.log('有人连上来了');
socket.ii=++ii;
channel[ii]=socket;
//监听新用户加入
socket.on('login', function(obj){
console.log(obj);
});
socket.on('disconnect', function(){
console.log(socket.ii+'离开了');
});
//监听用户发布聊天内容
socket.on('message', function(obj){
console.log(obj);
//socket.send(ii+'说:'+obj);
for(var key in channel){
channel[key].send(socket.ii+'说:'+obj);
}
});
})
//--------------------------------------------------

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

posted @ 2017-08-29 15:00  开始战斗  阅读(1004)  评论(0编辑  收藏  举报