-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Closed
Description
You want to:
- report a bug
- request a feature
Current behavior
TypeError: this.client.packet in not a function.
Steps to reproduce (if the current behavior is a bug)
I use socket.io-react and it works fine on the client side and client can emit events to the server
Client
index.js
import React from 'react'
import ReactDOM from 'react-dom'
import registerServiceWorker from './registerServiceWorker'
.....
import io from 'socket.io-client'
import {SocketProvider} from 'socket.io-react'
const socket = io.connect('http://localhost:8524')
socket.on('message', msg => console.log(msg))
ReactDOM.render(
<MuiThemeProvider>
<BrowserRouter>
<SocketProvider socket={socket}>
<Switch>
...
</Switch>
</SocketProvider>
</BrowserRouter>
</MuiThemeProvider>
, document.getElementById('root'));
registerServiceWorker();some where elese:
EventEmitter.on('loggedin', (token) => {
this.props.socket.emit('login', token)
this.getData()
this.forceUpdate()
})
Server
index.js
import Server from './server'
import Sticky from 'sticky-session'
const PORT = 8524 || parseInt(process.env['port'])
if(!Sticky.listen(Server, PORT)){
Server.once('listening', function() {
console.log('server started on ' + PORT + ' port')
})
}server.js
import HTTP from 'http'
import SocketIO from 'socket.io'
import WS from '../websocket'
...
const App = Express()
, Server = HTTP.createServer(App)
, IO = SocketIO(Server)
// Add headers
App.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://127.0.0.1:3002')
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,x-token,authorization,Authorization,content-type')
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true)
res.setHeader('Access-Control-Expose-Headers', 'x-token,Authorization')
// Pass to next layer of middleware
next()
})
....
IO.on('connect', WS)
App.all('*', function(req, res, next){
if(!res.headerSent)
res.status(404).send('Not found.')
})
export default Serverwebsocket.js
...
function handler(socket){
console.log('client connected')
// even this code will produce error:
socket.emit('sth')
socket.on('login', function(token){
function handleValue(data){
socket.user = data.user
socket.role = data.role
socket.client = data.client
socket.emit('login', 'ok')
}
function fromKnex(){
Knex('auth_keys')
.where('token', token)
.first('role', 'user', 'client')
.then(row => {
if(!row || !row.user)
return socket.emit('login', 'nok')
Redis.hset('tokens', token, JSON.stringify(row))
handleValue(row)
})
}
Redis.hget('tokens', token, (err, res) => {
if(err || !res)
return fromKnex()
handleValue(JSON.parse(res))
})
})
...
}
export default handlerExpected behavior
It should work fine!
Setup
- OS: Fedora 25
- browser: Chromium 59.0.3071.104 Fedora Project
- socket.io version: 2.0.4
- npm version: 5.4.2
- node version: 8.2.1
Other information (e.g. stacktraces, related issues, suggestions how to fix)
I guess this issue is related to Sticky-session or ES6
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels