Commit 02c023b3 by Zhang Xin

socket test

parent 71b93041
import fetch from 'isomorphic-fetch'
import actionType from '../actiontype/getIp'
let domain = 'http://192.168.10.100:7788';
let domainIp = '192.168.10.';
let ipIndex = 0;
const saveIp = (data)=>{
return{
type : actionType.SAVEIP,
data
}
};
const checkStatus = (response)=>{
console.log("===========")
console.log(response);
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
};
const parseJson = (response)=>{
return response.json();
}
const getIp = ()=>{
return (dispatch =>{
console.log("ipIndex",ipIndex)
if(ipIndex > 255){
}else{
let url = 'http://' + domainIp + ipIndex + ':7788/testIp';
console.log("url : ",url);
fetch( url ,{
// credentials : 'include',
method : 'HEAD',
mode : 'cors',
body : {}
}).then(checkStatus)
.then(parseJson)
.then((data) => {
console.log("data :",data);
dispatch(saveIp(domainIp + ipIndex + ':7788'))
})
.catch((err) =>{
console.log("Err:",err);
});
}
})
};
export {getIp}
const actionTypes = {
SAVEIP : 'SAVEIP'
}
export default actionTypes;
\ No newline at end of file
'use strict'; 'use strict';
import React from 'react' import React from 'react'
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import {getIp} from '../../actions/getIp'
class PageContainer extends React.Component { class PageContainer extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
UTILPATH.socket.init()
} }
componentWillMount() { componentWillMount() {
let {dispatch} = this.props;
dispatch(getIp());
} }
componentDidMount(){ componentDidMount(){
} }
......
...@@ -5,8 +5,7 @@ import { Provider } from 'react-redux'; ...@@ -5,8 +5,7 @@ import { Provider } from 'react-redux';
import thunkMiddleware from 'redux-thunk'; import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger'; import createLogger from 'redux-logger';
import reducers from './reducers/index'; import reducers from './reducers/index';
import {Router, hashHistory} from 'react-router'; import PageContainer from './containers/PageContainer/PageContainer'
import routes from './routes/index'
require('./index.css'); require('./index.css');
// init thunk // init thunk
...@@ -25,8 +24,7 @@ function activateVendor() { ...@@ -25,8 +24,7 @@ function activateVendor() {
function renderPage(store) { function renderPage(store) {
ReactDOM.render( ReactDOM.render(
<Provider store={store}> <Provider store={store}>
<Router history={hashHistory} routes={routes}> <PageContainer />
</Router>
</Provider> </Provider>
, ,
document.getElementById('root') document.getElementById('root')
......
import actionTypes from '../actiontype/getIp';
const saveIp = (content,data)=>{
return Object.assign({},content,{ip:data});
}
export default function(state={}, action) {
switch (action.type) {
case actionTypes.SAVEIP:
return saveIp(state,action.data)
default:
return state;
}
}
\ No newline at end of file
import headlogo from '../image/header-logo.png'; import headlogo from '../image/header-logo.png';
import banner1 from '../image/banner1.png';
import banner2 from '../image/banner2.png';
import banner3 from '../image/banner3.png';
import defaultImg from '../image/default.png'; import defaultImg from '../image/default.png';
const localHost = 'http://localhost:7788'; const localHost = 'http://localhost:7788';
...@@ -76,6 +73,6 @@ const handleImgLoad = (ref)=>{ ...@@ -76,6 +73,6 @@ const handleImgLoad = (ref)=>{
return style; return style;
} }
const localImg = {headlogo,banner1,banner2,banner3,defaultImg} const localImg = {headlogo,defaultImg}
module.exports = {localHost,devHost,masterHost,getPic,handleImgError,handleImgLoad,localImg}; module.exports = {localHost,devHost,masterHost,getPic,handleImgError,handleImgLoad,localImg};
\ No newline at end of file
let domainIp = '192.168.10.';
let ipIndex = 0;
const init = ()=>{
if("WebSocket" in window){
console.log("ipIndex : " , ipIndex)
let url = 'ws://' + domainIp + ipIndex + ':7788/notify';
try{
let ws = new WebSocket(url);
ws.onopen = function()
{
// Web Socket 已连接上,使用 send() 方法发送数据
// ws.send("发送数据");
console.log("open");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
console.log("数据已接收...");
};
ws.onclose = function(err)
{
// 关闭 websocket
console.log("连接已关闭...");
console.log("err : ",err)
};
ws.onerror = function (err) {
console.log("wrong...");
console.log("err : ",err)
ipIndex++;
ipIndex = ipIndex > 255 ? 0 : ipIndex;
init();
}
}catch (e) {
console.log("ERR : ", e);
}
}
};
const error = ()=>{
};
module.exports = {init}
\ No newline at end of file
import image from './image'; import image from './image';
module.exports = {...image}; import socket from './socket';
\ No newline at end of file module.exports = {...image,socket};
\ No newline at end of file
...@@ -93,7 +93,6 @@ var envConfig = { ...@@ -93,7 +93,6 @@ var envConfig = {
output: { output: {
path: path.join(__dirname, './static'), path: path.join(__dirname, './static'),
filename: 'bundle-[hash:6].js', filename: 'bundle-[hash:6].js',
chunkFilename: '[name].[chunkhash:5].chunk.js',
}, },
plugins: [ plugins: [
new webpack.optimize.UglifyJsPlugin(), new webpack.optimize.UglifyJsPlugin(),
...@@ -115,11 +114,7 @@ var envConfig = { ...@@ -115,11 +114,7 @@ var envConfig = {
output: { output: {
path: path.join(__dirname, './static'), path: path.join(__dirname, './static'),
filename: 'bundle.js', filename: 'bundle.js',
chunkFilename: '[name].[chunkhash:5].chunk.js',
}, },
// externals:{
// 'BMap':'BMap'
// },
plugins: [ plugins: [
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'), new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js'),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
...@@ -136,7 +131,6 @@ var envConfig = { ...@@ -136,7 +131,6 @@ var envConfig = {
output: { output: {
path: path.join(__dirname, './static'), path: path.join(__dirname, './static'),
filename: 'bundle-[hash:6].js', filename: 'bundle-[hash:6].js',
chunkFilename: '[name].[chunkhash:5].chunk.js',
}, },
plugins: [ plugins: [
new webpack.optimize.UglifyJsPlugin(), new webpack.optimize.UglifyJsPlugin(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment