在尝试设置一个简单的NodeJS服务器和Socket.io客户端来测试WebSockets的东西时,我偶然发现了一些愚蠢的东西。我很确定这是一件很蠢的事情我已经完成了因为我以前曾经使用过NodeJS / Socket.io而且从来没有遇到过这个问题。
使用下面的代码,我能够收到“' tick'来自客户端服务器的事件,但服务器似乎无法接收到' ping'来自客户的事件。 '蜱'用于确保服务器 - >客户端工作,并且' ping'测试客户端 - >服务器。
使用最新的Socket.io(1.4.6)和express(4.14.0)
server.js:
@Override
public void postInit(Scene scene) {
// additional setUp logic
boolean showReadingView = (boolean) PlatformProvider.getPlatform().getLaunchIntentExtra("showReadingView", false);
if (showReadingView) {
switchView(READING_VIEW);
}
}
的index.html:
var express = require('express');
var app = require('express')();
var server = require('http').createServer(app);
var sio = require('socket.io')(server);
var path = require('path');
app.use(express.static(path.join(__dirname, 'public_html')));
// Socket.io
sio.on('connection', (socket) => {
// Store socket ID
var socketID = socket.conn.id;
// Log connection
console.log('Connection:', socketID);
// Ping event
socket.on('ping', (message) => {
console.log('Ping:', socketID, '-', (message || '(no message>'));
});
// Tick event
var tick = function(){
var now = new Date().getTime().toString();
socket.emit('tick', now);
}
setInterval(tick, 5000);
// Disconnect event
socket.on('disconnect', () => {
console.log('Disconnected:', socketID);
});
});
server.listen(4100, () => {
console.log('Listening on :4100');
});
答案 0 :(得分:0)
希望我理解你的问题......
我使用socket.io
做了一些聊天工作,所以它可能对你有帮助。
这是:
<div class="searchBox" style="height: 600px ; width: 500px">
<div style=";width:400px;border-right:1px solid black;;overflow:scroll-y;">
<b style="color: black ;text-decoration: underline">USERS:</b>
<div id="users" style="color: black"></div>
</div>
<div style=";width:300px;height:250px;overflow:scroll-y;padding:10px;">
<b style="color: black ; text-decoration: underline">CONVERSATION:</b>
<div id="conversation" style="color: black"></div>
<input id="data" style="width:200px;" />
<button id="datasend" style="color: #0f0f0f;">send</button>
</div>
和js:
var socket = io.connect('http://localhost:8080');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
socket.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
服务器:
var app = require('./app');
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8080, function() {
console.log("Gym Project is listening to: http://127.0.0.1:8080");
});
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
io.sockets.on('connection', function (socket) {
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
这是与一些客户的非常简单的聊天...... 可能有帮助:)
答案 1 :(得分:0)
无法判断Socket.io是否采用了ping
事件,或者以下行导致了问题:
console.log('Ping:', socketID, '-', (message || '(no message)'));
无论哪种方式,通过将事件名称更改为ev:ping
(这更容易理解),并简化该行已修复! :)