2016-03-22 31 views
0

新しい値が回復するたびにnode.jsサーバーにデータを送信するpythonスクリプトが1つあります。ReferenceError:socketが定義されていません

は、ここに私のコードのクライアント側(Pythonスクリプト)です。ここで

from socketIO_client import SocketIO 

... 

def update_db(dateUpdate, res, nameIndex): 
    try: 
     with SocketIO('192.168.100.103', 8080) as socketIO: 
      socketIO.emit('newValues', res) 

    except Exception as e: 
     print "error socketIO - Impossible to emit data \n" 

    #Update database 

while True: 
    #If new value 
     dateUpdate = time.strftime('%Y-%m-%d %H:%M:%S') 
     update_db(dateUpdate, res, nameIndex[i]) 
     break 

だ私のNode.jsサーバー:

var path = require('path'); 
var fs = require('fs'); 
var mysql = require("mysql"); 
var express = require('express'); 

var app = express(); 

var server = app.listen(8080); 
console.log('Server listening on port 8080'); 

var io = require('socket.io')(server); 

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

    socket.on('newValues', function (data) { 
      console.log("new values : "+data);  
    }); 
}); 

問題:

私のPythonスクリプトは、新しい受け取っEache時間データを送信しなければなりません。私のノードサーバーは停止し、このエラーを表示します:

node server.js 

Server listening on port 8080 
client connected 
C:\project\src\server\server.js:26 

socket.on('newValues', function (data) { 
    ^

ReferenceError: socket is not defined 
    at Namespace.<anonymous> (C:\project\src\server\server.js:26:5) 
    at emitOne (events.js:90:13) 
    at Namespace.emit (events.js:182:7) 
    at Namespace.emit (C:project\node_modules\socket.io\lib\nam 
espace.js:206:10) 
    at C:\project\node_modules\socket.io\lib\namespace.js:174:14 

    at nextTickCallbackWith0Args (node.js:452:9) 
    at process._tickCallback (node.js:381:13) 

誰でもこのエラーをご存知でしょうか?あなたは

socket.on('newValues' , ...) 

を書くとき

答えて

4

ソケットが定義されていません。ソケットをio.sockets.onコールバック関数に渡す必要があります。

io.sockets.on('connection', function (socket) { //Pass socket here 
    console.log("client connected"); 

    socket.on('newValues', function (data) { 
     console.log("new values : "+data);  
    }); 
} 

編集:これは私の最初の一見のベスト推測ソリューションです。私はsockets.ioでの経験はあまりありません。私はドキュメントを見ているだけです(このリンクにも同様の例があります)http://socket.io/docs/

0

"ソケット" は変数であると仮定しています。もしそうであれば、その呼び出しの前に定義されていません。

私はよくIOを知っているが、代わりにこれを試してみません:

io.sockets.on('newValues' , ...) 
関連する問題